diff --git a/Client/Entities/Sync/OnFootSync.cs b/Client/Entities/Sync/OnFootSync.cs index ed6760f..98c835c 100644 --- a/Client/Entities/Sync/OnFootSync.cs +++ b/Client/Entities/Sync/OnFootSync.cs @@ -48,8 +48,6 @@ namespace CoopClient.Entities Character.IsInvincible = false; Function.Call(Hash.START_ENTITY_FIRE, Character.Handle); - - return; } else if (!IsOnFire && Character.IsOnFire) { @@ -74,8 +72,6 @@ namespace CoopClient.Entities { Character.CanRagdoll = true; Character.Ragdoll(); - - return; } else if (!IsRagdoll && Character.IsRagdoll) { @@ -83,8 +79,9 @@ namespace CoopClient.Entities Character.CanRagdoll = false; } - if (IsJumping || IsOnFire) + if (IsJumping || IsOnFire || IsRagdoll) { + UpdateOnFootPosition(true, true, false); return; } @@ -96,10 +93,8 @@ namespace CoopClient.Entities Character.Task.ReloadWeapon(); } - if (Character.IsInRange(Position, 0.5f)) - { - return; - } + UpdateOnFootPosition(); + return; } if (Character.Weapons.Current.Hash != (WeaponHash)CurrentWeaponHash || !WeaponComponents.Compare(LastWeaponComponents)) @@ -166,57 +161,72 @@ namespace CoopClient.Entities private bool LastMoving; private void WalkTo() { - if (!Character.IsInRange(Position, 6.0f) && (LastMoving = true)) + Vector3 predictPosition = Position + (Position - Character.Position) + Velocity; + float range = predictPosition.DistanceToSquared(Character.Position); + + switch (Speed) { - Character.Position = Position; - Character.Rotation = Rotation; + case 1: + if (!Character.IsWalking || range > 0.25f) + { + float nrange = range * 2; + if (nrange > 1.0f) + { + nrange = 1.0f; + } + + Character.Task.GoStraightTo(predictPosition); + Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, nrange); + } + LastMoving = true; + break; + case 2: + if (!Character.IsRunning || range > 0.50f) + { + Character.Task.RunTo(predictPosition, true); + Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f); + } + LastMoving = true; + break; + case 3: + if (!Character.IsSprinting || range > 0.75f) + { + Function.Call(Hash.TASK_GO_STRAIGHT_TO_COORD, Character.Handle, predictPosition.X, predictPosition.Y, predictPosition.Z, 3.0f, -1, 0.0f, 0.0f); + Function.Call(Hash.SET_RUN_SPRINT_MULTIPLIER_FOR_PLAYER, Character.Handle, 1.49f); + Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f); + } + LastMoving = true; + break; + default: + if (LastMoving) + { + Character.Task.StandStill(2000); + LastMoving = false; + } + break; } - else + UpdateOnFootPosition(); + } + + private void UpdateOnFootPosition(bool updatePosition = true, bool updateRotation = true, bool updateVelocity = true) + { + if (updatePosition) { - Vector3 predictPosition = Position + (Position - Character.Position) + Velocity; - float range = predictPosition.DistanceToSquared(Character.Position); + float lerpValue = ((int)((Latency * 1000 / 2) + Main.MainNetworking.Latency * 1000 / 2)) * 2 / 50000f; - switch (Speed) - { - case 1: - if ((!Character.IsWalking || range > 0.25f) && (LastMoving = true)) - { - float nrange = range * 2; - if (nrange > 1.0f) - { - nrange = 1.0f; - } - - Character.Task.GoStraightTo(predictPosition); - Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, nrange); - } - break; - case 2: - if ((!Character.IsRunning || range > 0.50f) && (LastMoving = true)) - { - Character.Task.RunTo(predictPosition, true); - Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f); - } - break; - case 3: - if ((!Character.IsSprinting || range > 0.75f) && (LastMoving = true)) - { - Function.Call(Hash.TASK_GO_STRAIGHT_TO_COORD, Character.Handle, predictPosition.X, predictPosition.Y, predictPosition.Z, 3.0f, -1, 0.0f, 0.0f); - Function.Call(Hash.SET_RUN_SPRINT_MULTIPLIER_FOR_PLAYER, Character.Handle, 1.49f); - Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f); - } - break; - default: - if (!Character.IsInRange(Position, 0.5f)) - { - Character.Task.RunTo(Position, true, 500); - } - else if (LastMoving && (LastMoving = false)) - { - Character.Task.StandStill(1000); - } - break; - } + Vector2 biDimensionalPos = Vector2.Lerp(new Vector2(Character.Position.X, Character.Position.Y), new Vector2(Position.X + (Velocity.X / 5), Position.Y + (Velocity.Y / 5)), lerpValue); + float zPos = Util.Lerp(Character.Position.Z, Position.Z, 0.1f); + Character.PositionNoOffset = new Vector3(biDimensionalPos.X, biDimensionalPos.Y, zPos); + } + + if (updateRotation) + { + Character.Rotation = Vector3.Lerp(Character.Rotation, Rotation, 0.10f); + } + + if (updateVelocity) + { + Character.Velocity = Velocity; } } } diff --git a/Client/Main.cs b/Client/Main.cs index 91dcf27..5752879 100644 --- a/Client/Main.cs +++ b/Client/Main.cs @@ -275,7 +275,7 @@ namespace CoopClient DebugSyncPed = Players[0]; } - if ((Util.GetTickCount64() - ArtificialLagCounter) < 56) + if ((Util.GetTickCount64() - ArtificialLagCounter) < 30) { return; } diff --git a/Client/Packets.cs b/Client/Packets.cs index a680404..099e0f8 100644 --- a/Client/Packets.cs +++ b/Client/Packets.cs @@ -482,10 +482,10 @@ namespace CoopClient // Write the count of clothes byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); // Loop the dictionary and add the values - for (int i = 0; i < Clothes.Count; i++) + foreach (KeyValuePair cloth in Clothes) { - // Write the cloth value - byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); + byteArray.Add(cloth.Key); + byteArray.AddRange(BitConverter.GetBytes(cloth.Value)); } // Write player velocity @@ -517,6 +517,7 @@ namespace CoopClient byteArray.AddRange(BitConverter.GetBytes((ushort)WeaponComponents.Count)); foreach (KeyValuePair component in WeaponComponents) { + byteArray.AddRange(BitConverter.GetBytes(component.Key)); byteArray.AddRange(BitConverter.GetBytes(component.Value)); } } @@ -587,8 +588,7 @@ namespace CoopClient for (ushort i = 0; i < clothCount; i++) { // Read cloth value - short clothValue = reader.ReadShort(); - Clothes.Add((byte)i, clothValue); + Clothes.Add(reader.ReadByte(), reader.ReadShort()); } // Read player velocity @@ -624,10 +624,9 @@ namespace CoopClient { WeaponComponents = new Dictionary(); ushort comCount = reader.ReadUShort(); - for (ushort i = 0; i < comCount; i++) { - WeaponComponents.Add(i, reader.ReadBool()); + WeaponComponents.Add(reader.ReadUInt(), reader.ReadBool()); } } @@ -706,10 +705,10 @@ namespace CoopClient // Write the count of clothes byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); // Loop the dictionary and add the values - for (int i = 0; i < Clothes.Count; i++) + foreach (KeyValuePair cloth in Clothes) { - // Write the cloth value - byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); + byteArray.Add(cloth.Key); + byteArray.AddRange(BitConverter.GetBytes(cloth.Value)); } // Write vehicle model hash @@ -775,7 +774,8 @@ namespace CoopClient foreach (KeyValuePair mod in VehMods) { // Write the mod value - byteArray.AddRange(BitConverter.GetBytes((short)mod.Value)); + byteArray.AddRange(BitConverter.GetBytes(mod.Key)); + byteArray.AddRange(BitConverter.GetBytes(mod.Value)); } if (!VehDamageModel.Equals(default(VehicleDamageModel))) @@ -836,11 +836,10 @@ namespace CoopClient // Read the count of clothes ushort clothCount = reader.ReadUShort(); // For clothCount - for (int i = 0; i < clothCount; i++) + for (ushort i = 0; i < clothCount; i++) { // Read cloth value - short clothValue = reader.ReadShort(); - Clothes.Add((byte)i, clothValue); + Clothes.Add(reader.ReadByte(), reader.ReadShort()); } // Read vehicle model hash @@ -921,8 +920,7 @@ namespace CoopClient for (int i = 0; i < vehModCount; i++) { // Read the mod value - short mod = reader.ReadShort(); - VehMods.Add(i, mod); + VehMods.Add(reader.ReadInt(), reader.ReadInt()); } if (reader.ReadBool()) @@ -1667,10 +1665,10 @@ namespace CoopClient // Write the count of clothes byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); // Loop the dictionary and add the values - for (int i = 0; i < Clothes.Count; i++) + foreach (KeyValuePair cloth in Clothes) { - // Write the cloth value - byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); + byteArray.Add(cloth.Key); + byteArray.AddRange(BitConverter.GetBytes(cloth.Value)); } // Write npc health @@ -1749,8 +1747,7 @@ namespace CoopClient for (ushort i = 0; i < clothCount; i++) { // Read cloth value - short clothValue = reader.ReadShort(); - Clothes.Add((byte)i, clothValue); + Clothes.Add(reader.ReadByte(), reader.ReadShort()); } // Read npc health @@ -1853,10 +1850,10 @@ namespace CoopClient // Write the count of clothes byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); // Loop the dictionary and add the values - for (int i = 0; i < Clothes.Count; i++) + foreach (KeyValuePair cloth in Clothes) { - // Write the cloth value - byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); + byteArray.Add(cloth.Key); + byteArray.AddRange(BitConverter.GetBytes(cloth.Value)); } // Write vehicle model hash @@ -1914,7 +1911,8 @@ namespace CoopClient foreach (KeyValuePair mod in VehMods) { // Write the mod value - byteArray.AddRange(BitConverter.GetBytes((short)mod.Value)); + byteArray.AddRange(BitConverter.GetBytes(mod.Key)); + byteArray.AddRange(BitConverter.GetBytes(mod.Value)); } if (!VehDamageModel.Equals(default(VehicleDamageModel))) @@ -1974,8 +1972,7 @@ namespace CoopClient for (int i = 0; i < clothCount; i++) { // Read cloth value - short clothValue = reader.ReadShort(); - Clothes.Add((byte)i, clothValue); + Clothes.Add(reader.ReadByte(), reader.ReadShort()); } // Read vehicle model hash @@ -2045,8 +2042,7 @@ namespace CoopClient for (int i = 0; i < vehModCount; i++) { // Read the mod value - short mod = reader.ReadShort(); - VehMods.Add(i, mod); + VehMods.Add(reader.ReadInt(), reader.ReadInt()); } if (reader.ReadBool()) diff --git a/Client/Util.cs b/Client/Util.cs index 1e6eb16..b0b440d 100644 --- a/Client/Util.cs +++ b/Client/Util.cs @@ -47,6 +47,12 @@ namespace CoopClient } #endregion + // Dirty & dangerous + public static dynamic Lerp(dynamic from, dynamic to, float fAlpha) + { + return ((to - from) * fAlpha + from); + } + public static T GetGameMs() where T : IConvertible { return (T)Convert.ChangeType(1f / (Game.FPS > 60f ? 60f : Game.FPS) * 1000f, typeof(T)); diff --git a/Server/Packets.cs b/Server/Packets.cs index 7d4ff38..f6a0903 100644 --- a/Server/Packets.cs +++ b/Server/Packets.cs @@ -422,10 +422,10 @@ namespace CoopServer // Write the count of clothes byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); // Loop the dictionary and add the values - for (int i = 0; i < Clothes.Count; i++) + foreach (KeyValuePair cloth in Clothes) { - // Write the cloth value - byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); + byteArray.Add(cloth.Key); + byteArray.AddRange(BitConverter.GetBytes(cloth.Value)); } // Write player velocity @@ -457,6 +457,7 @@ namespace CoopServer byteArray.AddRange(BitConverter.GetBytes((ushort)WeaponComponents.Count)); foreach (KeyValuePair component in WeaponComponents) { + byteArray.AddRange(BitConverter.GetBytes(component.Key)); byteArray.AddRange(BitConverter.GetBytes(component.Value)); } } @@ -527,8 +528,7 @@ namespace CoopServer for (ushort i = 0; i < clothCount; i++) { // Read cloth value - short clothValue = reader.ReadShort(); - Clothes.Add((byte)i, clothValue); + Clothes.Add(reader.ReadByte(), reader.ReadShort()); } // Read player velocity @@ -564,10 +564,9 @@ namespace CoopServer { WeaponComponents = new Dictionary(); ushort comCount = reader.ReadUShort(); - for (ushort i = 0; i < comCount; i++) { - WeaponComponents.Add(i, reader.ReadBool()); + WeaponComponents.Add(reader.ReadUInt(), reader.ReadBool()); } } @@ -646,10 +645,10 @@ namespace CoopServer // Write the count of clothes byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); // Loop the dictionary and add the values - for (int i = 0; i < Clothes.Count; i++) + foreach (KeyValuePair cloth in Clothes) { - // Write the cloth value - byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); + byteArray.Add(cloth.Key); + byteArray.AddRange(BitConverter.GetBytes(cloth.Value)); } // Write vehicle model hash @@ -715,7 +714,8 @@ namespace CoopServer foreach (KeyValuePair mod in VehMods) { // Write the mod value - byteArray.AddRange(BitConverter.GetBytes((short)mod.Value)); + byteArray.AddRange(BitConverter.GetBytes(mod.Key)); + byteArray.AddRange(BitConverter.GetBytes(mod.Value)); } if (!VehDamageModel.Equals(default(VehicleDamageModel))) @@ -776,11 +776,10 @@ namespace CoopServer // Read the count of clothes ushort clothCount = reader.ReadUShort(); // For clothCount - for (int i = 0; i < clothCount; i++) + for (ushort i = 0; i < clothCount; i++) { // Read cloth value - short clothValue = reader.ReadShort(); - Clothes.Add((byte)i, clothValue); + Clothes.Add(reader.ReadByte(), reader.ReadShort()); } // Read vehicle model hash @@ -861,8 +860,7 @@ namespace CoopServer for (int i = 0; i < vehModCount; i++) { // Read the mod value - short mod = reader.ReadShort(); - VehMods.Add(i, mod); + VehMods.Add(reader.ReadInt(), reader.ReadInt()); } if (reader.ReadBool()) @@ -1607,10 +1605,10 @@ namespace CoopServer // Write the count of clothes byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); // Loop the dictionary and add the values - for (int i = 0; i < Clothes.Count; i++) + foreach (KeyValuePair cloth in Clothes) { - // Write the cloth value - byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); + byteArray.Add(cloth.Key); + byteArray.AddRange(BitConverter.GetBytes(cloth.Value)); } // Write npc health @@ -1689,8 +1687,7 @@ namespace CoopServer for (ushort i = 0; i < clothCount; i++) { // Read cloth value - short clothValue = reader.ReadShort(); - Clothes.Add((byte)i, clothValue); + Clothes.Add(reader.ReadByte(), reader.ReadShort()); } // Read npc health @@ -1793,10 +1790,10 @@ namespace CoopServer // Write the count of clothes byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); // Loop the dictionary and add the values - for (int i = 0; i < Clothes.Count; i++) + foreach (KeyValuePair cloth in Clothes) { - // Write the cloth value - byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); + byteArray.Add(cloth.Key); + byteArray.AddRange(BitConverter.GetBytes(cloth.Value)); } // Write vehicle model hash @@ -1854,7 +1851,8 @@ namespace CoopServer foreach (KeyValuePair mod in VehMods) { // Write the mod value - byteArray.AddRange(BitConverter.GetBytes((short)mod.Value)); + byteArray.AddRange(BitConverter.GetBytes(mod.Key)); + byteArray.AddRange(BitConverter.GetBytes(mod.Value)); } if (!VehDamageModel.Equals(default(VehicleDamageModel))) @@ -1914,8 +1912,7 @@ namespace CoopServer for (int i = 0; i < clothCount; i++) { // Read cloth value - short clothValue = reader.ReadShort(); - Clothes.Add((byte)i, clothValue); + Clothes.Add(reader.ReadByte(), reader.ReadShort()); } // Read vehicle model hash @@ -1985,8 +1982,7 @@ namespace CoopServer for (int i = 0; i < vehModCount; i++) { // Read the mod value - short mod = reader.ReadShort(); - VehMods.Add(i, mod); + VehMods.Add(reader.ReadInt(), reader.ReadInt()); } if (reader.ReadBool())