Bug fixes and new player movement

This commit is contained in:
EntenKoeniq 2021-12-24 03:16:16 +01:00
parent 96d2a9ca4c
commit b9703dd612
5 changed files with 123 additions and 115 deletions

View File

@ -48,8 +48,6 @@ namespace CoopClient.Entities
Character.IsInvincible = false; Character.IsInvincible = false;
Function.Call(Hash.START_ENTITY_FIRE, Character.Handle); Function.Call(Hash.START_ENTITY_FIRE, Character.Handle);
return;
} }
else if (!IsOnFire && Character.IsOnFire) else if (!IsOnFire && Character.IsOnFire)
{ {
@ -74,8 +72,6 @@ namespace CoopClient.Entities
{ {
Character.CanRagdoll = true; Character.CanRagdoll = true;
Character.Ragdoll(); Character.Ragdoll();
return;
} }
else if (!IsRagdoll && Character.IsRagdoll) else if (!IsRagdoll && Character.IsRagdoll)
{ {
@ -83,8 +79,9 @@ namespace CoopClient.Entities
Character.CanRagdoll = false; Character.CanRagdoll = false;
} }
if (IsJumping || IsOnFire) if (IsJumping || IsOnFire || IsRagdoll)
{ {
UpdateOnFootPosition(true, true, false);
return; return;
} }
@ -96,11 +93,9 @@ namespace CoopClient.Entities
Character.Task.ReloadWeapon(); Character.Task.ReloadWeapon();
} }
if (Character.IsInRange(Position, 0.5f)) UpdateOnFootPosition();
{
return; return;
} }
}
if (Character.Weapons.Current.Hash != (WeaponHash)CurrentWeaponHash || !WeaponComponents.Compare(LastWeaponComponents)) if (Character.Weapons.Current.Hash != (WeaponHash)CurrentWeaponHash || !WeaponComponents.Compare(LastWeaponComponents))
{ {
@ -165,13 +160,6 @@ namespace CoopClient.Entities
private bool LastMoving; private bool LastMoving;
private void WalkTo() private void WalkTo()
{
if (!Character.IsInRange(Position, 6.0f) && (LastMoving = true))
{
Character.Position = Position;
Character.Rotation = Rotation;
}
else
{ {
Vector3 predictPosition = Position + (Position - Character.Position) + Velocity; Vector3 predictPosition = Position + (Position - Character.Position) + Velocity;
float range = predictPosition.DistanceToSquared(Character.Position); float range = predictPosition.DistanceToSquared(Character.Position);
@ -179,7 +167,7 @@ namespace CoopClient.Entities
switch (Speed) switch (Speed)
{ {
case 1: case 1:
if ((!Character.IsWalking || range > 0.25f) && (LastMoving = true)) if (!Character.IsWalking || range > 0.25f)
{ {
float nrange = range * 2; float nrange = range * 2;
if (nrange > 1.0f) if (nrange > 1.0f)
@ -190,33 +178,55 @@ namespace CoopClient.Entities
Character.Task.GoStraightTo(predictPosition); Character.Task.GoStraightTo(predictPosition);
Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, nrange); Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, nrange);
} }
LastMoving = true;
break; break;
case 2: case 2:
if ((!Character.IsRunning || range > 0.50f) && (LastMoving = true)) if (!Character.IsRunning || range > 0.50f)
{ {
Character.Task.RunTo(predictPosition, true); Character.Task.RunTo(predictPosition, true);
Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f); Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f);
} }
LastMoving = true;
break; break;
case 3: case 3:
if ((!Character.IsSprinting || range > 0.75f) && (LastMoving = true)) 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.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_RUN_SPRINT_MULTIPLIER_FOR_PLAYER, Character.Handle, 1.49f);
Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f); Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f);
} }
LastMoving = true;
break; break;
default: default:
if (!Character.IsInRange(Position, 0.5f)) if (LastMoving)
{ {
Character.Task.RunTo(Position, true, 500); Character.Task.StandStill(2000);
} LastMoving = false;
else if (LastMoving && (LastMoving = false))
{
Character.Task.StandStill(1000);
} }
break; break;
} }
UpdateOnFootPosition();
}
private void UpdateOnFootPosition(bool updatePosition = true, bool updateRotation = true, bool updateVelocity = true)
{
if (updatePosition)
{
float lerpValue = ((int)((Latency * 1000 / 2) + Main.MainNetworking.Latency * 1000 / 2)) * 2 / 50000f;
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;
} }
} }
} }

View File

@ -275,7 +275,7 @@ namespace CoopClient
DebugSyncPed = Players[0]; DebugSyncPed = Players[0];
} }
if ((Util.GetTickCount64() - ArtificialLagCounter) < 56) if ((Util.GetTickCount64() - ArtificialLagCounter) < 30)
{ {
return; return;
} }

View File

@ -482,10 +482,10 @@ namespace CoopClient
// Write the count of clothes // Write the count of clothes
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
// Loop the dictionary and add the values // Loop the dictionary and add the values
for (int i = 0; i < Clothes.Count; i++) foreach (KeyValuePair<byte, short> cloth in Clothes)
{ {
// Write the cloth value byteArray.Add(cloth.Key);
byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
} }
// Write player velocity // Write player velocity
@ -517,6 +517,7 @@ namespace CoopClient
byteArray.AddRange(BitConverter.GetBytes((ushort)WeaponComponents.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)WeaponComponents.Count));
foreach (KeyValuePair<uint, bool> component in WeaponComponents) foreach (KeyValuePair<uint, bool> component in WeaponComponents)
{ {
byteArray.AddRange(BitConverter.GetBytes(component.Key));
byteArray.AddRange(BitConverter.GetBytes(component.Value)); byteArray.AddRange(BitConverter.GetBytes(component.Value));
} }
} }
@ -587,8 +588,7 @@ namespace CoopClient
for (ushort i = 0; i < clothCount; i++) for (ushort i = 0; i < clothCount; i++)
{ {
// Read cloth value // Read cloth value
short clothValue = reader.ReadShort(); Clothes.Add(reader.ReadByte(), reader.ReadShort());
Clothes.Add((byte)i, clothValue);
} }
// Read player velocity // Read player velocity
@ -624,10 +624,9 @@ namespace CoopClient
{ {
WeaponComponents = new Dictionary<uint, bool>(); WeaponComponents = new Dictionary<uint, bool>();
ushort comCount = reader.ReadUShort(); ushort comCount = reader.ReadUShort();
for (ushort i = 0; i < comCount; i++) 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 // Write the count of clothes
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
// Loop the dictionary and add the values // Loop the dictionary and add the values
for (int i = 0; i < Clothes.Count; i++) foreach (KeyValuePair<byte, short> cloth in Clothes)
{ {
// Write the cloth value byteArray.Add(cloth.Key);
byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
} }
// Write vehicle model hash // Write vehicle model hash
@ -775,7 +774,8 @@ namespace CoopClient
foreach (KeyValuePair<int, int> mod in VehMods) foreach (KeyValuePair<int, int> mod in VehMods)
{ {
// Write the mod value // 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))) if (!VehDamageModel.Equals(default(VehicleDamageModel)))
@ -836,11 +836,10 @@ namespace CoopClient
// Read the count of clothes // Read the count of clothes
ushort clothCount = reader.ReadUShort(); ushort clothCount = reader.ReadUShort();
// For clothCount // For clothCount
for (int i = 0; i < clothCount; i++) for (ushort i = 0; i < clothCount; i++)
{ {
// Read cloth value // Read cloth value
short clothValue = reader.ReadShort(); Clothes.Add(reader.ReadByte(), reader.ReadShort());
Clothes.Add((byte)i, clothValue);
} }
// Read vehicle model hash // Read vehicle model hash
@ -921,8 +920,7 @@ namespace CoopClient
for (int i = 0; i < vehModCount; i++) for (int i = 0; i < vehModCount; i++)
{ {
// Read the mod value // Read the mod value
short mod = reader.ReadShort(); VehMods.Add(reader.ReadInt(), reader.ReadInt());
VehMods.Add(i, mod);
} }
if (reader.ReadBool()) if (reader.ReadBool())
@ -1667,10 +1665,10 @@ namespace CoopClient
// Write the count of clothes // Write the count of clothes
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
// Loop the dictionary and add the values // Loop the dictionary and add the values
for (int i = 0; i < Clothes.Count; i++) foreach (KeyValuePair<byte, short> cloth in Clothes)
{ {
// Write the cloth value byteArray.Add(cloth.Key);
byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
} }
// Write npc health // Write npc health
@ -1749,8 +1747,7 @@ namespace CoopClient
for (ushort i = 0; i < clothCount; i++) for (ushort i = 0; i < clothCount; i++)
{ {
// Read cloth value // Read cloth value
short clothValue = reader.ReadShort(); Clothes.Add(reader.ReadByte(), reader.ReadShort());
Clothes.Add((byte)i, clothValue);
} }
// Read npc health // Read npc health
@ -1853,10 +1850,10 @@ namespace CoopClient
// Write the count of clothes // Write the count of clothes
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
// Loop the dictionary and add the values // Loop the dictionary and add the values
for (int i = 0; i < Clothes.Count; i++) foreach (KeyValuePair<byte, short> cloth in Clothes)
{ {
// Write the cloth value byteArray.Add(cloth.Key);
byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
} }
// Write vehicle model hash // Write vehicle model hash
@ -1914,7 +1911,8 @@ namespace CoopClient
foreach (KeyValuePair<int, int> mod in VehMods) foreach (KeyValuePair<int, int> mod in VehMods)
{ {
// Write the mod value // 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))) if (!VehDamageModel.Equals(default(VehicleDamageModel)))
@ -1974,8 +1972,7 @@ namespace CoopClient
for (int i = 0; i < clothCount; i++) for (int i = 0; i < clothCount; i++)
{ {
// Read cloth value // Read cloth value
short clothValue = reader.ReadShort(); Clothes.Add(reader.ReadByte(), reader.ReadShort());
Clothes.Add((byte)i, clothValue);
} }
// Read vehicle model hash // Read vehicle model hash
@ -2045,8 +2042,7 @@ namespace CoopClient
for (int i = 0; i < vehModCount; i++) for (int i = 0; i < vehModCount; i++)
{ {
// Read the mod value // Read the mod value
short mod = reader.ReadShort(); VehMods.Add(reader.ReadInt(), reader.ReadInt());
VehMods.Add(i, mod);
} }
if (reader.ReadBool()) if (reader.ReadBool())

View File

@ -47,6 +47,12 @@ namespace CoopClient
} }
#endregion #endregion
// Dirty & dangerous
public static dynamic Lerp(dynamic from, dynamic to, float fAlpha)
{
return ((to - from) * fAlpha + from);
}
public static T GetGameMs<T>() where T : IConvertible public static T GetGameMs<T>() where T : IConvertible
{ {
return (T)Convert.ChangeType(1f / (Game.FPS > 60f ? 60f : Game.FPS) * 1000f, typeof(T)); return (T)Convert.ChangeType(1f / (Game.FPS > 60f ? 60f : Game.FPS) * 1000f, typeof(T));

View File

@ -422,10 +422,10 @@ namespace CoopServer
// Write the count of clothes // Write the count of clothes
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
// Loop the dictionary and add the values // Loop the dictionary and add the values
for (int i = 0; i < Clothes.Count; i++) foreach (KeyValuePair<byte, short> cloth in Clothes)
{ {
// Write the cloth value byteArray.Add(cloth.Key);
byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
} }
// Write player velocity // Write player velocity
@ -457,6 +457,7 @@ namespace CoopServer
byteArray.AddRange(BitConverter.GetBytes((ushort)WeaponComponents.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)WeaponComponents.Count));
foreach (KeyValuePair<uint, bool> component in WeaponComponents) foreach (KeyValuePair<uint, bool> component in WeaponComponents)
{ {
byteArray.AddRange(BitConverter.GetBytes(component.Key));
byteArray.AddRange(BitConverter.GetBytes(component.Value)); byteArray.AddRange(BitConverter.GetBytes(component.Value));
} }
} }
@ -527,8 +528,7 @@ namespace CoopServer
for (ushort i = 0; i < clothCount; i++) for (ushort i = 0; i < clothCount; i++)
{ {
// Read cloth value // Read cloth value
short clothValue = reader.ReadShort(); Clothes.Add(reader.ReadByte(), reader.ReadShort());
Clothes.Add((byte)i, clothValue);
} }
// Read player velocity // Read player velocity
@ -564,10 +564,9 @@ namespace CoopServer
{ {
WeaponComponents = new Dictionary<uint, bool>(); WeaponComponents = new Dictionary<uint, bool>();
ushort comCount = reader.ReadUShort(); ushort comCount = reader.ReadUShort();
for (ushort i = 0; i < comCount; i++) 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 // Write the count of clothes
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
// Loop the dictionary and add the values // Loop the dictionary and add the values
for (int i = 0; i < Clothes.Count; i++) foreach (KeyValuePair<byte, short> cloth in Clothes)
{ {
// Write the cloth value byteArray.Add(cloth.Key);
byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
} }
// Write vehicle model hash // Write vehicle model hash
@ -715,7 +714,8 @@ namespace CoopServer
foreach (KeyValuePair<int, int> mod in VehMods) foreach (KeyValuePair<int, int> mod in VehMods)
{ {
// Write the mod value // 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))) if (!VehDamageModel.Equals(default(VehicleDamageModel)))
@ -776,11 +776,10 @@ namespace CoopServer
// Read the count of clothes // Read the count of clothes
ushort clothCount = reader.ReadUShort(); ushort clothCount = reader.ReadUShort();
// For clothCount // For clothCount
for (int i = 0; i < clothCount; i++) for (ushort i = 0; i < clothCount; i++)
{ {
// Read cloth value // Read cloth value
short clothValue = reader.ReadShort(); Clothes.Add(reader.ReadByte(), reader.ReadShort());
Clothes.Add((byte)i, clothValue);
} }
// Read vehicle model hash // Read vehicle model hash
@ -861,8 +860,7 @@ namespace CoopServer
for (int i = 0; i < vehModCount; i++) for (int i = 0; i < vehModCount; i++)
{ {
// Read the mod value // Read the mod value
short mod = reader.ReadShort(); VehMods.Add(reader.ReadInt(), reader.ReadInt());
VehMods.Add(i, mod);
} }
if (reader.ReadBool()) if (reader.ReadBool())
@ -1607,10 +1605,10 @@ namespace CoopServer
// Write the count of clothes // Write the count of clothes
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
// Loop the dictionary and add the values // Loop the dictionary and add the values
for (int i = 0; i < Clothes.Count; i++) foreach (KeyValuePair<byte, short> cloth in Clothes)
{ {
// Write the cloth value byteArray.Add(cloth.Key);
byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
} }
// Write npc health // Write npc health
@ -1689,8 +1687,7 @@ namespace CoopServer
for (ushort i = 0; i < clothCount; i++) for (ushort i = 0; i < clothCount; i++)
{ {
// Read cloth value // Read cloth value
short clothValue = reader.ReadShort(); Clothes.Add(reader.ReadByte(), reader.ReadShort());
Clothes.Add((byte)i, clothValue);
} }
// Read npc health // Read npc health
@ -1793,10 +1790,10 @@ namespace CoopServer
// Write the count of clothes // Write the count of clothes
byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count)); byteArray.AddRange(BitConverter.GetBytes((ushort)Clothes.Count));
// Loop the dictionary and add the values // Loop the dictionary and add the values
for (int i = 0; i < Clothes.Count; i++) foreach (KeyValuePair<byte, short> cloth in Clothes)
{ {
// Write the cloth value byteArray.Add(cloth.Key);
byteArray.AddRange(BitConverter.GetBytes(Clothes[(byte)i])); byteArray.AddRange(BitConverter.GetBytes(cloth.Value));
} }
// Write vehicle model hash // Write vehicle model hash
@ -1854,7 +1851,8 @@ namespace CoopServer
foreach (KeyValuePair<int, int> mod in VehMods) foreach (KeyValuePair<int, int> mod in VehMods)
{ {
// Write the mod value // 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))) if (!VehDamageModel.Equals(default(VehicleDamageModel)))
@ -1914,8 +1912,7 @@ namespace CoopServer
for (int i = 0; i < clothCount; i++) for (int i = 0; i < clothCount; i++)
{ {
// Read cloth value // Read cloth value
short clothValue = reader.ReadShort(); Clothes.Add(reader.ReadByte(), reader.ReadShort());
Clothes.Add((byte)i, clothValue);
} }
// Read vehicle model hash // Read vehicle model hash
@ -1985,8 +1982,7 @@ namespace CoopServer
for (int i = 0; i < vehModCount; i++) for (int i = 0; i < vehModCount; i++)
{ {
// Read the mod value // Read the mod value
short mod = reader.ReadShort(); VehMods.Add(reader.ReadInt(), reader.ReadInt());
VehMods.Add(i, mod);
} }
if (reader.ReadBool()) if (reader.ReadBool())