Rope overwrite client with server rope

This commit is contained in:
Sveske_Juice 2024-03-04 18:08:18 +01:00
parent add18eb38c
commit 2a9000b120
2 changed files with 56 additions and 40 deletions

View File

@ -30,7 +30,7 @@ public class Rope
return sum; return sum;
} }
public static float CalcDiff(Rope r1, Rope r2) public static float CalcDiff(Rope r1, Rope r2, bool excludeZ = true)
{ {
if (r1.points.Length != r2.points.Length) if (r1.points.Length != r2.points.Length)
{ {
@ -38,10 +38,23 @@ public class Rope
} }
float diff = 0; float diff = 0;
if (excludeZ)
{
for (int i = 0; i < r1.points.Length; i++)
{
Vector3 p1, p2;
p1 = new Vector2(r1.points[i].position.x, r1.points[i].position.y);
p2 = new Vector2(r2.points[i].position.x, r2.points[i].position.y);
diff += Vector2.Distance(p1, p2);
}
}
else
{
for (int i = 0; i < r1.points.Length; i++) for (int i = 0; i < r1.points.Length; i++)
{ {
diff += Vector3.Distance(r1.points[i].position, r2.points[i].position); diff += Vector3.Distance(r1.points[i].position, r2.points[i].position);
} }
}
return diff; return diff;
} }
@ -53,11 +66,11 @@ public class Rope
prevPositions = rope.points.Select(p => p.prevPosition).ToArray(), prevPositions = rope.points.Select(p => p.prevPosition).ToArray(),
locked = rope.points.Select(p => p.locked).ToArray(), locked = rope.points.Select(p => p.locked).ToArray(),
sPosA = rope.sticks.Select(s => s.A.position).ToArray(), // sPosA = rope.sticks.Select(s => s.A.position).ToArray(),
sPrevPosA = rope.sticks.Select(s => s.A.prevPosition).ToArray(), // sPrevPosA = rope.sticks.Select(s => s.A.prevPosition).ToArray(),
sPosB = rope.sticks.Select(s => s.B.position).ToArray(), // sPosB = rope.sticks.Select(s => s.B.position).ToArray(),
sPrevPosB = rope.sticks.Select(s => s.B.prevPosition).ToArray(), // sPrevPosB = rope.sticks.Select(s => s.B.prevPosition).ToArray(),
dead = rope.sticks.Select(s => s.dead).ToArray(), // dead = rope.sticks.Select(s => s.dead).ToArray(),
}; };
} }
@ -132,24 +145,24 @@ public struct NetworkRope : INetworkSerializable
public bool[] locked; public bool[] locked;
// For rope sticks // For rope sticks
public Vector3[] sPosA; // public Vector3[] sPosA;
public Vector3[] sPrevPosA; // public Vector3[] sPrevPosA;
//
public Vector3[] sPosB; // public Vector3[] sPosB;
public Vector3[] sPrevPosB; // public Vector3[] sPrevPosB;
//
public bool[] dead; // public bool[] dead;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{ {
int positionsLen = 0; int positionsLen = 0;
int prevPositionsLen = 0; int prevPositionsLen = 0;
int lockedLen = 0; int lockedLen = 0;
int sPosALen = 0; // int sPosALen = 0;
int sPrevPosALen = 0; // int sPrevPosALen = 0;
int sPosBLen = 0; // int sPosBLen = 0;
int sPrevPosBLen = 0; // int sPrevPosBLen = 0;
int deadLen = 0; // int deadLen = 0;
if (!serializer.IsReader) if (!serializer.IsReader)
{ {
@ -157,21 +170,21 @@ public struct NetworkRope : INetworkSerializable
prevPositionsLen = prevPositions.Length; prevPositionsLen = prevPositions.Length;
lockedLen = locked.Length; lockedLen = locked.Length;
sPosALen = sPosA.Length; // sPosALen = sPosA.Length;
sPrevPosALen = sPrevPosA.Length; // sPrevPosALen = sPrevPosA.Length;
sPosBLen = sPosB.Length; // sPosBLen = sPosB.Length;
sPrevPosBLen = sPrevPosB.Length; // sPrevPosBLen = sPrevPosB.Length;
deadLen = dead.Length; // deadLen = dead.Length;
} }
serializer.SerializeValue(ref positionsLen); serializer.SerializeValue(ref positionsLen);
serializer.SerializeValue(ref prevPositionsLen); serializer.SerializeValue(ref prevPositionsLen);
serializer.SerializeValue(ref lockedLen); serializer.SerializeValue(ref lockedLen);
serializer.SerializeValue(ref sPosALen); // serializer.SerializeValue(ref sPosALen);
serializer.SerializeValue(ref sPrevPosALen); // serializer.SerializeValue(ref sPrevPosALen);
serializer.SerializeValue(ref sPosBLen); // serializer.SerializeValue(ref sPosBLen);
serializer.SerializeValue(ref sPrevPosBLen); // serializer.SerializeValue(ref sPrevPosBLen);
serializer.SerializeValue(ref deadLen); // serializer.SerializeValue(ref deadLen);
if (serializer.IsReader) if (serializer.IsReader)
{ {
@ -179,11 +192,11 @@ public struct NetworkRope : INetworkSerializable
prevPositions = new Vector3[prevPositionsLen]; prevPositions = new Vector3[prevPositionsLen];
locked = new bool[lockedLen]; locked = new bool[lockedLen];
sPosA = new Vector3[sPosALen]; // sPosA = new Vector3[sPosALen];
sPrevPosA = new Vector3[sPrevPosALen]; // sPrevPosA = new Vector3[sPrevPosALen];
sPosB = new Vector3[sPosBLen]; // sPosB = new Vector3[sPosBLen];
sPrevPosB = new Vector3[sPrevPosBLen]; // sPrevPosB = new Vector3[sPrevPosBLen];
dead = new bool[deadLen]; // dead = new bool[deadLen];
} }
for (int i = 0; i < positionsLen; i++) for (int i = 0; i < positionsLen; i++)
@ -201,7 +214,7 @@ public struct NetworkRope : INetworkSerializable
serializer.SerializeValue(ref locked[i]); serializer.SerializeValue(ref locked[i]);
} }
for (int i = 0; i < sPosALen; i++) /* for (int i = 0; i < sPosALen; i++)
{ {
serializer.SerializeValue(ref sPosA[i]); serializer.SerializeValue(ref sPosA[i]);
} }
@ -224,6 +237,6 @@ public struct NetworkRope : INetworkSerializable
for (int i = 0; i < deadLen; i++) for (int i = 0; i < deadLen; i++)
{ {
serializer.SerializeValue(ref dead[i]); serializer.SerializeValue(ref dead[i]);
} } */
} }
} }

View File

@ -66,7 +66,7 @@ public class RopeSimulator : NetworkBehaviour
[Header("Netcode")] [Header("Netcode")]
private const int k_rngSeed = 6969; private const int k_rngSeed = 6969;
private const float k_sendRopeDataRPCThreshold = 0.1f; private const float k_sendRopeDataRPCThreshold = 0.1f;
private const float k_ropeReconciliateThreshold = 0.1f; private const float k_ropeReconciliateThreshold = 0.25f;
private System.Random rng = new System.Random(k_rngSeed); private System.Random rng = new System.Random(k_rngSeed);
private int[] order; private int[] order;
@ -280,12 +280,15 @@ public class RopeSimulator : NetworkBehaviour
private void ServerRopeDataReceivedRpc(NetworkRope nrope) private void ServerRopeDataReceivedRpc(NetworkRope nrope)
{ {
Debug.Log($"Received rope data from server: {nrope}"); Debug.Log($"Received rope data from server: {nrope}");
Debug.Log(nrope.sPosA[1]);
Rope serverRope = Rope.FromNetworkRope(nrope, distBetweenRopePoints); Rope serverRope = Rope.FromNetworkRope(nrope, distBetweenRopePoints);
if (Rope.CalcDiff(this.rope, serverRope) > k_ropeReconciliateThreshold)
float diff = Rope.CalcDiff(this.rope, serverRope);
Debug.Log(diff);
if (diff > k_ropeReconciliateThreshold)
{ {
Debug.LogWarning("Reconciliating rope!"); Debug.LogWarning("Reconciliating rope!");
this.rope = serverRope; this.rope = serverRope;
Debug.Log(Rope.CalcDiff(this.rope, serverRope));
} }
} }