From f14cce82841af3bba28fe99255df400da176649d Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Fri, 14 Jun 2024 13:38:12 +0200 Subject: [PATCH] Rework damage packets --- .../Minefield/Minefield_method_2_Patch.cs | 19 ++- Fika.Core/Coop/Players/CoopPlayer.cs | 30 ++--- Fika.Core/Coop/Players/ObservedCoopPlayer.cs | 126 ++++++++---------- Fika.Core/Networking/FikaSerialization.cs | 78 ++--------- .../Networking/Packets/Player/DamagePacket.cs | 59 +++++++- 5 files changed, 142 insertions(+), 170 deletions(-) diff --git a/Fika.Core/Coop/Patches/Minefield/Minefield_method_2_Patch.cs b/Fika.Core/Coop/Patches/Minefield/Minefield_method_2_Patch.cs index 61fa23a3..8855767e 100644 --- a/Fika.Core/Coop/Patches/Minefield/Minefield_method_2_Patch.cs +++ b/Fika.Core/Coop/Patches/Minefield/Minefield_method_2_Patch.cs @@ -76,17 +76,14 @@ private static void DoReplicatedMineDamage(IPlayer player, float distance, bool { coopPlayer.PacketSender.DamagePackets.Enqueue(new() { - DamageInfo = new() - { - DamageType = EDamageType.Landmine, - Damage = num4 * num2, - ArmorDamage = 0.5f, - PenetrationPower = 30f, - Direction = Vector3.zero, - HitNormal = Vector3.zero, - ColliderType = bodyPartCollider.BodyPartColliderType, - BodyPartType = bodyPartCollider.BodyPartType - } + DamageType = EDamageType.Landmine, + Damage = num4 * num2, + ArmorDamage = 0.5f, + PenetrationPower = 30f, + Direction = Vector3.zero, + HitNormal = Vector3.zero, + ColliderType = bodyPartCollider.BodyPartColliderType, + BodyPartType = bodyPartCollider.BodyPartType }); if (++num5 >= num3) { diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index 78f954e2..dde59a24 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -1273,22 +1273,22 @@ public virtual void HandleDamagePacket(ref DamagePacket packet) { DamageInfo damageInfo = new() { - Damage = packet.DamageInfo.Damage, - DamageType = packet.DamageInfo.DamageType, - BodyPartColliderType = packet.DamageInfo.ColliderType, - HitPoint = packet.DamageInfo.Point, - HitNormal = packet.DamageInfo.HitNormal, - Direction = packet.DamageInfo.Direction, - PenetrationPower = packet.DamageInfo.PenetrationPower, - BlockedBy = packet.DamageInfo.BlockedBy, - DeflectedBy = packet.DamageInfo.DeflectedBy, - SourceId = packet.DamageInfo.SourceId, - ArmorDamage = packet.DamageInfo.ArmorDamage + Damage = packet.Damage, + DamageType = packet.DamageType, + BodyPartColliderType = packet.ColliderType, + HitPoint = packet.Point, + HitNormal = packet.HitNormal, + Direction = packet.Direction, + PenetrationPower = packet.PenetrationPower, + BlockedBy = packet.BlockedBy, + DeflectedBy = packet.DeflectedBy, + SourceId = packet.SourceId, + ArmorDamage = packet.ArmorDamage }; - if (!string.IsNullOrEmpty(packet.DamageInfo.ProfileId)) + if (!string.IsNullOrEmpty(packet.ProfileId)) { - GInterface106 player = Singleton.Instance.GetAlivePlayerBridgeByProfileID(packet.DamageInfo.ProfileId); + GInterface106 player = Singleton.Instance.GetAlivePlayerBridgeByProfileID(packet.ProfileId); if (player != null) { @@ -1302,13 +1302,13 @@ public virtual void HandleDamagePacket(ref DamagePacket packet) } } - if (Singleton.Instance.GetAlivePlayerByProfileID(packet.DamageInfo.ProfileId).HandsController.Item is Weapon weapon) + if (Singleton.Instance.GetAlivePlayerByProfileID(packet.ProfileId).HandsController.Item is Weapon weapon) { damageInfo.Weapon = weapon; } } - ClientApplyShot(damageInfo, packet.DamageInfo.BodyPartType, packet.DamageInfo.ColliderType, packet.DamageInfo.ArmorPlateCollider); + ClientApplyShot(damageInfo, packet.BodyPartType, packet.ColliderType, packet.ArmorPlateCollider); } public virtual void SetupDogTag() diff --git a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs index 67fce72d..35bda856 100644 --- a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs +++ b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs @@ -264,22 +264,19 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy { PacketSender.DamagePackets.Enqueue(new() { - DamageInfo = new() - { - Damage = damageInfo.Damage, - DamageType = damageInfo.DamageType, - BodyPartType = bodyPartType, - ColliderType = colliderType, - Absorbed = 0f, - Direction = damageInfo.Direction, - Point = damageInfo.HitPoint, - HitNormal = damageInfo.HitNormal, - PenetrationPower = damageInfo.PenetrationPower, - BlockedBy = damageInfo.BlockedBy, - DeflectedBy = damageInfo.DeflectedBy, - SourceId = damageInfo.SourceId, - ArmorDamage = damageInfo.ArmorDamage - } + Damage = damageInfo.Damage, + DamageType = damageInfo.DamageType, + BodyPartType = bodyPartType, + ColliderType = colliderType, + Absorbed = 0f, + Direction = damageInfo.Direction, + Point = damageInfo.HitPoint, + HitNormal = damageInfo.HitNormal, + PenetrationPower = damageInfo.PenetrationPower, + BlockedBy = damageInfo.BlockedBy, + DeflectedBy = damageInfo.DeflectedBy, + SourceId = damageInfo.SourceId, + ArmorDamage = damageInfo.ArmorDamage }); return; @@ -336,23 +333,20 @@ public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy ShotReactions(damageInfo, bodyPartType); PacketSender.DamagePackets.Enqueue(new() { - DamageInfo = new() - { - Damage = damageInfo.Damage, - DamageType = damageInfo.DamageType, - BodyPartType = bodyPartType, - ColliderType = colliderType, - ArmorPlateCollider = armorPlateCollider, - Absorbed = 0f, - Direction = damageInfo.Direction, - Point = damageInfo.HitPoint, - HitNormal = damageInfo.HitNormal, - PenetrationPower = damageInfo.PenetrationPower, - BlockedBy = damageInfo.BlockedBy, - DeflectedBy = damageInfo.DeflectedBy, - SourceId = damageInfo.SourceId, - ArmorDamage = damageInfo.ArmorDamage - } + Damage = damageInfo.Damage, + DamageType = damageInfo.DamageType, + BodyPartType = bodyPartType, + ColliderType = colliderType, + ArmorPlateCollider = armorPlateCollider, + Absorbed = 0f, + Direction = damageInfo.Direction, + Point = damageInfo.HitPoint, + HitNormal = damageInfo.HitNormal, + PenetrationPower = damageInfo.PenetrationPower, + BlockedBy = damageInfo.BlockedBy, + DeflectedBy = damageInfo.DeflectedBy, + SourceId = damageInfo.SourceId, + ArmorDamage = damageInfo.ArmorDamage }); return null; @@ -404,24 +398,21 @@ public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy PacketSender.DamagePackets.Enqueue(new() { - DamageInfo = new() - { - Damage = damage, - DamageType = damageInfo.DamageType, - BodyPartType = bodyPartType, - ColliderType = colliderType, - ArmorPlateCollider = armorPlateCollider, - Absorbed = 0f, - Direction = damageInfo.Direction, - Point = damageInfo.HitPoint, - HitNormal = damageInfo.HitNormal, - PenetrationPower = damageInfo.PenetrationPower, - BlockedBy = damageInfo.BlockedBy, - DeflectedBy = damageInfo.DeflectedBy, - SourceId = damageInfo.SourceId, - ArmorDamage = damageInfo.ArmorDamage, - ProfileId = damageInfo.Player.iPlayer.ProfileId - } + Damage = damage, + DamageType = damageInfo.DamageType, + BodyPartType = bodyPartType, + ColliderType = colliderType, + ArmorPlateCollider = armorPlateCollider, + Absorbed = 0f, + Direction = damageInfo.Direction, + Point = damageInfo.HitPoint, + HitNormal = damageInfo.HitNormal, + PenetrationPower = damageInfo.PenetrationPower, + BlockedBy = damageInfo.BlockedBy, + DeflectedBy = damageInfo.DeflectedBy, + SourceId = damageInfo.SourceId, + ArmorDamage = damageInfo.ArmorDamage, + ProfileId = damageInfo.Player.iPlayer.ProfileId }); // Run this to get weapon skill @@ -464,24 +455,21 @@ public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy PacketSender.DamagePackets.Enqueue(new() { - DamageInfo = new() - { - Damage = damage, - DamageType = damageInfo.DamageType, - BodyPartType = bodyPartType, - ColliderType = colliderType, - ArmorPlateCollider = armorPlateCollider, - Absorbed = 0f, - Direction = damageInfo.Direction, - Point = damageInfo.HitPoint, - HitNormal = damageInfo.HitNormal, - PenetrationPower = damageInfo.PenetrationPower, - BlockedBy = damageInfo.BlockedBy, - DeflectedBy = damageInfo.DeflectedBy, - SourceId = damageInfo.SourceId, - ArmorDamage = damageInfo.ArmorDamage, - ProfileId = damageInfo.Player.iPlayer.ProfileId - } + Damage = damage, + DamageType = damageInfo.DamageType, + BodyPartType = bodyPartType, + ColliderType = colliderType, + ArmorPlateCollider = armorPlateCollider, + Absorbed = 0f, + Direction = damageInfo.Direction, + Point = damageInfo.HitPoint, + HitNormal = damageInfo.HitNormal, + PenetrationPower = damageInfo.PenetrationPower, + BlockedBy = damageInfo.BlockedBy, + DeflectedBy = damageInfo.DeflectedBy, + SourceId = damageInfo.SourceId, + ArmorDamage = damageInfo.ArmorDamage, + ProfileId = damageInfo.Player.iPlayer.ProfileId }); // Run this to get weapon skill diff --git a/Fika.Core/Networking/FikaSerialization.cs b/Fika.Core/Networking/FikaSerialization.cs index 558f02a5..a6644a75 100644 --- a/Fika.Core/Networking/FikaSerialization.cs +++ b/Fika.Core/Networking/FikaSerialization.cs @@ -273,8 +273,10 @@ public struct LightStatesPacket public FirearmLightStateStruct[] LightStates; public static LightStatesPacket Deserialize(NetDataReader reader) { - LightStatesPacket packet = new(); - packet.Amount = reader.GetInt(); + LightStatesPacket packet = new() + { + Amount = reader.GetInt() + }; if (packet.Amount > 0) { packet.LightStates = new FirearmLightStateStruct[packet.Amount]; @@ -312,8 +314,10 @@ public struct HeadLightsPacket public FirearmLightStateStruct[] LightStates; public static HeadLightsPacket Deserialize(NetDataReader reader) { - HeadLightsPacket packet = new(); - packet.Amount = reader.GetInt(); + HeadLightsPacket packet = new() + { + Amount = reader.GetInt() + }; if (packet.Amount > 0) { packet.LightStates = new FirearmLightStateStruct[packet.Amount]; @@ -631,72 +635,6 @@ public static void Serialize(NetDataWriter writer, GrenadePacket packet) } } - public struct ApplyShotPacket() - { - public EDamageType DamageType; - public float Damage; - public EBodyPart BodyPartType; - public EBodyPartColliderType ColliderType; - public EArmorPlateCollider ArmorPlateCollider; - public float Absorbed; - public Vector3 Direction = Vector3.zero; - public Vector3 Point = Vector3.zero; - public Vector3 HitNormal = Vector3.zero; - public float PenetrationPower = 0f; - public string BlockedBy; - public string DeflectedBy; - public string SourceId; - public string AmmoId; - public int FragmentIndex; - public float ArmorDamage = 0f; - public string ProfileId; - - public static ApplyShotPacket Deserialize(NetDataReader reader) - { - ApplyShotPacket packet = new() - { - DamageType = (EDamageType)reader.GetInt(), - Damage = reader.GetFloat(), - BodyPartType = (EBodyPart)reader.GetInt(), - ColliderType = (EBodyPartColliderType)reader.GetInt(), - ArmorPlateCollider = (EArmorPlateCollider)reader.GetInt(), - Absorbed = reader.GetFloat(), - Direction = reader.GetVector3(), - Point = reader.GetVector3(), - HitNormal = reader.GetVector3(), - PenetrationPower = reader.GetFloat(), - BlockedBy = reader.GetString(), - DeflectedBy = reader.GetString(), - SourceId = reader.GetString(), - AmmoId = reader.GetString(), - FragmentIndex = reader.GetInt(), - ArmorDamage = reader.GetFloat(), - ProfileId = reader.GetString() - }; - return packet; - } - public static void Serialize(NetDataWriter writer, ApplyShotPacket packet) - { - writer.Put((int)packet.DamageType); - writer.Put(packet.Damage); - writer.Put((int)packet.BodyPartType); - writer.Put((int)packet.ColliderType); - writer.Put((int)packet.ArmorPlateCollider); - writer.Put(packet.Absorbed); - writer.Put(packet.Direction); - writer.Put(packet.Point); - writer.Put(packet.HitNormal); - writer.Put(packet.PenetrationPower); - writer.Put(packet.BlockedBy); - writer.Put(packet.DeflectedBy); - writer.Put(packet.SourceId); - writer.Put(packet.AmmoId); - writer.Put(packet.FragmentIndex); - writer.Put(packet.ArmorDamage); - writer.Put(packet.ProfileId); - } - } - public struct ItemControllerExecutePacket { public uint CallbackId; diff --git a/Fika.Core/Networking/Packets/Player/DamagePacket.cs b/Fika.Core/Networking/Packets/Player/DamagePacket.cs index f1dcc5f2..e21a31c8 100644 --- a/Fika.Core/Networking/Packets/Player/DamagePacket.cs +++ b/Fika.Core/Networking/Packets/Player/DamagePacket.cs @@ -1,23 +1,72 @@ -using LiteNetLib.Utils; -using static Fika.Core.Networking.FikaSerialization; +using EFT; +using LiteNetLib.Utils; +using UnityEngine; namespace Fika.Core.Networking { public struct DamagePacket(int netId) : INetSerializable { public int NetId = netId; - public ApplyShotPacket DamageInfo; + public EDamageType DamageType; + public float Damage; + public EBodyPart BodyPartType; + public EBodyPartColliderType ColliderType; + public EArmorPlateCollider ArmorPlateCollider; + public float Absorbed; + public Vector3 Direction = Vector3.zero; + public Vector3 Point = Vector3.zero; + public Vector3 HitNormal = Vector3.zero; + public float PenetrationPower = 0f; + public string BlockedBy; + public string DeflectedBy; + public string SourceId; + public string AmmoId; + public int FragmentIndex; + public float ArmorDamage = 0f; + public string ProfileId; public void Deserialize(NetDataReader reader) { NetId = reader.GetInt(); - DamageInfo = ApplyShotPacket.Deserialize(reader); + DamageType = (EDamageType)reader.GetInt(); + Damage = reader.GetFloat(); + BodyPartType = (EBodyPart)reader.GetInt(); + ColliderType = (EBodyPartColliderType)reader.GetInt(); + ArmorPlateCollider = (EArmorPlateCollider)reader.GetInt(); + Absorbed = reader.GetFloat(); + Direction = reader.GetVector3(); + Point = reader.GetVector3(); + HitNormal = reader.GetVector3(); + PenetrationPower = reader.GetFloat(); + BlockedBy = reader.GetString(); + DeflectedBy = reader.GetString(); + SourceId = reader.GetString(); + AmmoId = reader.GetString(); + FragmentIndex = reader.GetInt(); + ArmorDamage = reader.GetFloat(); + ProfileId = reader.GetString(); } public void Serialize(NetDataWriter writer) { writer.Put(NetId); - ApplyShotPacket.Serialize(writer, DamageInfo); + writer.Put((int)DamageType); + writer.Put(Damage); + writer.Put((int)BodyPartType); + writer.Put((int)ColliderType); + writer.Put((int)ArmorPlateCollider); + writer.Put(Absorbed); + writer.Put(Direction); + writer.Put(Point); + writer.Put(HitNormal); + writer.Put(PenetrationPower); + writer.Put(BlockedBy); + writer.Put(DeflectedBy); + writer.Put(SourceId); + writer.Put(AmmoId); + writer.Put(FragmentIndex); + writer.Put(ArmorDamage); + writer.Put(ProfileId); } } }