NetworkEvent optimization (more frequent use of WriteRangedSingle)

This commit is contained in:
Regalis11
2015-10-25 00:06:32 +03:00
parent 3335f87f64
commit aeeae13b35
15 changed files with 84 additions and 56 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -518,8 +518,8 @@ namespace Barotrauma
if (wallAttack) if (wallAttack)
{ {
message.Write(wallAttackPos.X); message.WriteRangedSingle(wallAttackPos.X, -50.0f, 50.0f, 10);
message.Write(wallAttackPos.Y); message.WriteRangedSingle(wallAttackPos.Y, -50.0f, 50.0f, 10);
} }
//message.Write(Velocity.X); //message.Write(Velocity.X);
@@ -559,7 +559,9 @@ namespace Barotrauma
if (wallAttack) if (wallAttack)
{ {
newWallAttackPos = new Vector2(message.ReadFloat(), message.ReadFloat()); newWallAttackPos = new Vector2(
message.ReadRangedSingle(-50.0f, 50.0f, 10),
message.ReadRangedSingle(-50.0f, 50.0f, 10));
} }
//newVelocity = new Vector2(message.ReadFloat(), message.ReadFloat()); //newVelocity = new Vector2(message.ReadFloat(), message.ReadFloat());
+19 -16
View File
@@ -95,8 +95,11 @@ namespace Barotrauma
{ {
if (limb.ignoreCollisions) continue; if (limb.ignoreCollisions) continue;
message.Write(limb.body.SimPosition.X); if (limb.SimPosition.Length() > NetConfig.AllowedRagdollDistance) return false;
message.Write(limb.body.SimPosition.Y);
message.WriteRangedSingle(limb.body.SimPosition.X, -NetConfig.AllowedRagdollDistance, NetConfig.AllowedRagdollDistance, 16);
message.WriteRangedSingle(limb.body.SimPosition.Y, -NetConfig.AllowedRagdollDistance, NetConfig.AllowedRagdollDistance, 16);
message.Write(limb.body.Rotation); message.Write(limb.body.Rotation);
i++; i++;
@@ -111,14 +114,14 @@ namespace Barotrauma
message.Write((float)NetTime.Now); message.Write((float)NetTime.Now);
message.Write(AnimController.TargetDir == Direction.Right); message.Write(AnimController.TargetDir == Direction.Right);
message.WriteRangedSingle(MathHelper.Clamp(AnimController.TargetMovement.X, -10.0f, 10.0f), -10.0f, 10.0f, 16); message.WriteRangedSingle(AnimController.TargetMovement.X, -1.0f, 1.0f, 8);
message.WriteRangedSingle(MathHelper.Clamp(AnimController.TargetMovement.Y, -10.0f, 10.0f), -10.0f, 10.0f, 16); message.WriteRangedSingle(AnimController.TargetMovement.Y, -1.0f, 1.0f, 8);
message.Write(AnimController.RefLimb.SimPosition.X); message.WriteRangedSingle(AnimController.RefLimb.SimPosition.X, -NetConfig.AllowedRagdollDistance, NetConfig.AllowedRagdollDistance, 16);
message.Write(AnimController.RefLimb.SimPosition.Y); message.WriteRangedSingle(AnimController.RefLimb.SimPosition.X, -NetConfig.AllowedRagdollDistance, NetConfig.AllowedRagdollDistance, 16);
message.Write(AnimController.RefLimb.LinearVelocity.X); //message.Write(AnimController.RefLimb.LinearVelocity.X);
message.Write(AnimController.RefLimb.LinearVelocity.Y); //message.Write(AnimController.RefLimb.LinearVelocity.Y);
return true; return true;
} }
@@ -144,8 +147,8 @@ namespace Barotrauma
try try
{ {
limbPos.X = message.ReadFloat(); limbPos.X = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
limbPos.Y = message.ReadFloat(); limbPos.Y = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
rotation = message.ReadFloat(); rotation = message.ReadFloat();
} }
@@ -190,14 +193,14 @@ namespace Barotrauma
try try
{ {
targetDir = message.ReadBoolean(); targetDir = message.ReadBoolean();
targetMovement.X = message.ReadRangedSingle(-10.0f, 10.0f, 16); targetMovement.X = message.ReadRangedSingle(-1.0f, 1.0f, 8);
targetMovement.Y = message.ReadRangedSingle(-10.0f, 10.0f, 16); targetMovement.Y = message.ReadRangedSingle(-1.0f, 1.0f, 8);
pos.X = message.ReadFloat(); pos.X = message.ReadRangedSingle(-1.0f, 1.0f, 8);
pos.Y = message.ReadFloat(); pos.Y = message.ReadRangedSingle(-1.0f, 1.0f, 8);
vel.X = message.ReadFloat(); //vel.X = message.ReadFloat();
vel.Y = message.ReadFloat(); //vel.Y = message.ReadFloat();
} }
catch (Exception e) catch (Exception e)
+34 -19
View File
@@ -1157,11 +1157,14 @@ namespace Barotrauma
if (inventory == null) return false; if (inventory == null) return false;
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)
{ {
message.Write(limb.body.SimPosition.X); if (limb.SimPosition.Length() > NetConfig.CharacterIgnoreDistance) return false;
message.Write(limb.body.SimPosition.Y);
message.WriteRangedSingle(limb.body.SimPosition.X, -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);
@@ -1175,7 +1178,6 @@ namespace Barotrauma
message.Write((byte)((health/maxHealth)*255.0f)); message.Write((byte)((health/maxHealth)*255.0f));
message.Write((byte)(MathHelper.Clamp(oxygen * 2.55f, 0.0f, 255.0f))); message.Write((byte)(MathHelper.Clamp(oxygen * 2.55f, 0.0f, 255.0f)));
return true; return true;
case NetworkEventType.EntityUpdate: case NetworkEventType.EntityUpdate:
var hasInputs = var hasInputs =
@@ -1206,16 +1208,20 @@ namespace Barotrauma
if (secondaryHeld) if (secondaryHeld)
{ {
message.Write(cursorPosition.X); Vector2 relativeCursorPosition = cursorPosition - Position;
message.Write(cursorPosition.Y);
message.WriteRangedSingle(relativeCursorPosition.X, -2000.0f, 2000.0f, 8);
message.WriteRangedSingle(relativeCursorPosition.Y, -2000.0f, 2000.0f, 8);
} }
else else
{ {
message.Write(AnimController.Dir > 0.0f); message.Write(AnimController.Dir > 0.0f);
} }
message.Write(AnimController.RefLimb.SimPosition.X); if (AnimController.RefLimb.SimPosition.Length() > NetConfig.CharacterIgnoreDistance) return true;
message.Write(AnimController.RefLimb.SimPosition.Y);
message.WriteRangedSingle(AnimController.RefLimb.SimPosition.X, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
message.WriteRangedSingle(AnimController.RefLimb.SimPosition.Y, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
return true; return true;
default: default:
@@ -1298,8 +1304,8 @@ namespace Barotrauma
try try
{ {
limbPos.X = message.ReadFloat(); limbPos.X = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
limbPos.Y = message.ReadFloat(); limbPos.Y = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
rotation = message.ReadFloat(); rotation = message.ReadFloat();
} }
@@ -1335,7 +1341,7 @@ namespace Barotrauma
return; return;
case NetworkEventType.EntityUpdate: case NetworkEventType.EntityUpdate:
float sendingTime = 0.0f; float sendingTime = 0.0f;
Vector2 cursorPos = Vector2.Zero; Vector2 relativeCursorPos = Vector2.Zero;
bool actionKeyState, secondaryKeyState; bool actionKeyState, secondaryKeyState;
bool leftKeyState, rightKeyState, upKeyState, downKeyState; bool leftKeyState, rightKeyState, upKeyState, downKeyState;
@@ -1390,29 +1396,38 @@ namespace Barotrauma
{ {
if (secondaryKeyState) if (secondaryKeyState)
{ {
cursorPos = new Vector2( relativeCursorPos = new Vector2(
message.ReadFloat(), message.ReadRangedSingle(-2000.0f, 2000.0f, 8),
message.ReadFloat()); message.ReadRangedSingle(-2000.0f, 2000.0f, 8));
} }
else else
{ {
dir = message.ReadBoolean() ? 1.0f : -1.0f; dir = message.ReadBoolean() ? 1.0f : -1.0f;
} }
pos.X = message.ReadFloat();
pos.Y = message.ReadFloat();
} }
catch catch
{ {
#if DEBUG #if DEBUG
DebugConsole.ThrowError("Failed to read netowkrevent for "+this.ToString()); DebugConsole.ThrowError("Failed to read networkevent for "+this.ToString());
#endif #endif
return; return;
} }
try
{
pos.X = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
pos.Y = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
}
catch
{
//failed to read position, character may be further than NetConfig.CharacterIgnoreDistance
pos = SimPosition;
}
if (secondaryKeyState) if (secondaryKeyState)
{ {
cursorPosition = MathUtils.IsValid(cursorPos) ? cursorPos : Vector2.Zero; cursorPosition = MathUtils.IsValid(relativeCursorPos) ?
ConvertUnits.ToDisplayUnits(pos)+relativeCursorPos : Vector2.Zero;
} }
else else
{ {
@@ -168,8 +168,9 @@ namespace Barotrauma.Items.Components
public override void FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message) public override void FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message)
{ {
message.Write(Convert.ToByte(flowPercentage+100)); message.WriteRangedInteger(-10,10,(int)(flowPercentage/10.0f));
message.Write(IsActive); message.Write(IsActive);
message.WritePadBits();
} }
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message) public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message)
@@ -179,7 +180,7 @@ namespace Barotrauma.Items.Components
try try
{ {
newFlow = (float)(message.ReadByte()-100); newFlow = message.ReadRangedInteger(-10,10)*10.0f;
newActive = message.ReadBoolean(); newActive = message.ReadBoolean();
} }
@@ -427,7 +427,7 @@ namespace Barotrauma.Items.Components
{ {
message.Write(autoTemp); message.Write(autoTemp);
message.WriteRangedSingle(temperature, 0.0f, 10000.0f, 16); message.WriteRangedSingle(temperature, 0.0f, 10000.0f, 16);
message.WriteRangedSingle(shutDownTemp, 0.0f, 10000.0f, 16); message.WriteRangedSingle(shutDownTemp, 0.0f, 10000.0f, 7);
message.WriteRangedSingle(coolingRate, 0.0f, 100.0f, 8); message.WriteRangedSingle(coolingRate, 0.0f, 100.0f, 8);
message.WriteRangedSingle(fissionRate, 0.0f, 100.0f, 8); message.WriteRangedSingle(fissionRate, 0.0f, 100.0f, 8);
@@ -443,7 +443,8 @@ namespace Barotrauma.Items.Components
{ {
newAutoTemp = message.ReadBoolean(); newAutoTemp = message.ReadBoolean();
newTemperature = message.ReadRangedSingle(0.0f, 10000.0f, 16); newTemperature = message.ReadRangedSingle(0.0f, 10000.0f, 16);
newShutDownTemp = message.ReadRangedSingle(0.0f, 10000.0f, 16); newShutDownTemp = message.ReadRangedSingle(0.0f, 10000.0f, 7);
newShutDownTemp = MathUtils.Round(newShutDownTemp, 100.0f);
newCoolingRate = message.ReadRangedSingle(0.0f, 100.0f, 8); newCoolingRate = message.ReadRangedSingle(0.0f, 100.0f, 8);
newFissionRate = message.ReadRangedSingle(0.0f, 100.0f, 8); newFissionRate = message.ReadRangedSingle(0.0f, 100.0f, 8);
@@ -198,23 +198,25 @@ namespace Barotrauma.Items.Components
public override void FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message) public override void FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message)
{ {
message.Write((byte)((int)(rechargeSpeed/maxRechargeSpeed*255.0f))); message.WriteRangedSingle(MathHelper.Clamp(rechargeSpeed/MaxRechargeSpeed, 0.0f, 1.0f), 0.0f, 1.0f, 8);
message.Write(charge); message.WriteRangedSingle(MathHelper.Clamp(charge/capacity,0.0f, 1.0f), 0.0f, 1.0f, 8);
} }
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message) public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message)
{ {
byte newRechargeSpeed = 0; float newRechargeSpeed = 0f;
float newCharge = 0.0f; float newCharge = 0.0f;
try try
{ {
newRechargeSpeed = message.ReadByte(); newRechargeSpeed = message.ReadRangedSingle(0.0f, 1.0f, 8);
newCharge = message.ReadFloat(); newRechargeSpeed *= MaxRechargeSpeed;
newCharge = message.ReadRangedSingle(0.0f, 1.0f, 8);
newCharge *= capacity;
} }
catch { } catch { }
RechargeSpeed = (newRechargeSpeed/255.0f)*maxRechargeSpeed; RechargeSpeed = newRechargeSpeed;
Charge = newCharge; Charge = newCharge;
} }
+2 -2
View File
@@ -472,7 +472,7 @@ namespace Barotrauma
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message, object data) public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message, object data)
{ {
message.Write(volume); message.WriteRangedSingle(MathHelper.Clamp(volume/FullVolume, 0.0f, 1.5f), 0.0f, 1.5f, 6);
return true; return true;
} }
@@ -483,7 +483,7 @@ namespace Barotrauma
try try
{ {
newVolume = message.ReadFloat(); float newPercentage = message.ReadRangedSingle(0.0f, 1.5f, 6);
} }
catch catch
+2 -2
View File
@@ -653,7 +653,7 @@ namespace Barotrauma
message.Write((float)NetTime.Now); message.Write((float)NetTime.Now);
message.Write(byteIndex); message.Write(byteIndex);
message.Write(sections[sectionIndex].damage); message.WriteRangedSingle(sections[sectionIndex].damage/Health, 0.0f, 1.0f, 8);
return true; return true;
} }
@@ -668,7 +668,7 @@ namespace Barotrauma
{ {
updateTime = message.ReadFloat(); updateTime = message.ReadFloat();
sectionIndex = message.ReadByte(); sectionIndex = message.ReadByte();
damage = message.ReadFloat(); damage = message.ReadRangedSingle(0.0f, 1.0f, 8) * Health;
} }
catch catch
{ {
+1 -1
View File
@@ -314,7 +314,7 @@ namespace Barotrauma.Networking
{ {
if (c as AICharacter == null) continue; if (c as AICharacter == null) continue;
if (c.SimPosition.Length() > 100.0f) continue; if (c.SimPosition.Length() > NetConfig.CharacterIgnoreDistance) continue;
c.CreateUpdateNetworkEvent(false); c.CreateUpdateNetworkEvent(false);
} }
@@ -14,6 +14,10 @@ namespace Barotrauma.Networking
public static string MasterServerUrl = GameMain.Config.MasterServerUrl; public static string MasterServerUrl = GameMain.Config.MasterServerUrl;
//if a character is further than this from the sub, the server will ignore it
//(in sim units)
public const float CharacterIgnoreDistance = 100.0f;
//if a ragdoll is further than this from the correct position, teleport it there //if a ragdoll is further than this from the correct position, teleport it there
//(in sim units) //(in sim units)
public const float ResetRagdollDistance = 2.0f; public const float ResetRagdollDistance = 2.0f;
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.