From 6d0e83125c80d957c528d8db79c143f42e4917bc Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Fri, 14 Jun 2024 15:20:29 +0200 Subject: [PATCH] Rework damage and armor damage - Damage now sends damage after handling through armor - Armor damage is now synced to everyone else This should provide near live-like, if not more accurate, accuracy --- .../Coop/PacketHandlers/BotPacketSender.cs | 25 +++- .../Coop/PacketHandlers/ClientPacketSender.cs | 25 +++- .../Coop/PacketHandlers/IPacketSender.cs | 1 + .../PacketHandlers/ObservedPacketSender.cs | 39 +++++-- .../Coop/PacketHandlers/PacketReceiver.cs | 20 +++- .../Coop/PacketHandlers/ServerPacketSender.cs | 25 +++- Fika.Core/Coop/Players/CoopBot.cs | 14 +-- Fika.Core/Coop/Players/CoopPlayer.cs | 108 +++++++++++++++--- Fika.Core/Coop/Players/ObservedCoopPlayer.cs | 103 ++--------------- Fika.Core/Networking/FikaClient.cs | 9 ++ Fika.Core/Networking/FikaServer.cs | 12 ++ .../Packets/Player/ArmorDamagePacket.cs | 25 ++++ .../Networking/Packets/Player/DamagePacket.cs | 4 + 13 files changed, 257 insertions(+), 153 deletions(-) create mode 100644 Fika.Core/Networking/Packets/Player/ArmorDamagePacket.cs diff --git a/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs b/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs index 48dc98c8..dda7cfd5 100644 --- a/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs @@ -21,6 +21,7 @@ public class BotPacketSender : MonoBehaviour, IPacketSender public NetDataWriter Writer { get; set; } = new(); public Queue FirearmPackets { get; set; } = new(50); public Queue DamagePackets { get; set; } = new(50); + public Queue ArmorDamagePackets { get; set; } = new(50); public Queue InventoryPackets { get; set; } = new(50); public Queue CommonPlayerPackets { get; set; } = new(50); public Queue HealthSyncPackets { get; set; } = new(50); @@ -87,16 +88,28 @@ protected void Update() Server.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered); } } - int healthPackets = DamagePackets.Count; - if (healthPackets > 0) + int damagePackets = DamagePackets.Count; + if (damagePackets > 0) { - for (int i = 0; i < healthPackets; i++) + for (int i = 0; i < damagePackets; i++) { - DamagePacket healthPacket = DamagePackets.Dequeue(); - healthPacket.NetId = player.NetId; + DamagePacket damagePacket = DamagePackets.Dequeue(); + damagePacket.NetId = player.NetId; Writer.Reset(); - Server.SendDataToAll(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered); + Server.SendDataToAll(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered); + } + } + int armorDamagePackets = ArmorDamagePackets.Count; + if (armorDamagePackets > 0) + { + for (int i = 0; i < armorDamagePackets; i++) + { + ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue(); + armorDamagePacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered); } } int inventoryPackets = InventoryPackets.Count; diff --git a/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs index fd7c6fc2..0f65a5d0 100644 --- a/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs @@ -26,6 +26,7 @@ public class ClientPacketSender : MonoBehaviour, IPacketSender public NetDataWriter Writer { get; set; } = new(); public Queue FirearmPackets { get; set; } = new(50); public Queue DamagePackets { get; set; } = new(50); + public Queue ArmorDamagePackets { get; set; } = new(50); public Queue InventoryPackets { get; set; } = new(50); public Queue CommonPlayerPackets { get; set; } = new(50); public Queue HealthSyncPackets { get; set; } = new(50); @@ -95,16 +96,28 @@ protected void Update() Client.SendData(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered); } } - int healthPackets = DamagePackets.Count; - if (healthPackets > 0) + int damagePackets = DamagePackets.Count; + if (damagePackets > 0) { - for (int i = 0; i < healthPackets; i++) + for (int i = 0; i < damagePackets; i++) { - DamagePacket healthPacket = DamagePackets.Dequeue(); - healthPacket.NetId = player.NetId; + DamagePacket damagePacket = DamagePackets.Dequeue(); + damagePacket.NetId = player.NetId; Writer.Reset(); - Client.SendData(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered); + Client.SendData(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered); + } + } + int armorDamagePackets = ArmorDamagePackets.Count; + if (armorDamagePackets > 0) + { + for (int i = 0; i < armorDamagePackets; i++) + { + ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue(); + armorDamagePacket.NetId = player.NetId; + + Writer.Reset(); + Client.SendData(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered); } } int inventoryPackets = InventoryPackets.Count; diff --git a/Fika.Core/Coop/PacketHandlers/IPacketSender.cs b/Fika.Core/Coop/PacketHandlers/IPacketSender.cs index 25a034f8..27ad5e43 100644 --- a/Fika.Core/Coop/PacketHandlers/IPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/IPacketSender.cs @@ -15,6 +15,7 @@ public interface IPacketSender public NetDataWriter Writer { get; set; } public Queue FirearmPackets { get; set; } public Queue DamagePackets { get; set; } + public Queue ArmorDamagePackets { get; set; } public Queue InventoryPackets { get; set; } public Queue CommonPlayerPackets { get; set; } public Queue HealthSyncPackets { get; set; } diff --git a/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs index 7c07aa00..b81ed32a 100644 --- a/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs @@ -22,6 +22,7 @@ public class ObservedPacketSender : MonoBehaviour, IPacketSender public NetDataWriter Writer { get; set; } = new(); public Queue FirearmPackets { get; set; } = new(50); public Queue DamagePackets { get; set; } = new(50); + public Queue ArmorDamagePackets { get; set; } = new(50); public Queue InventoryPackets { get; set; } = new(50); public Queue CommonPlayerPackets { get; set; } = new(50); public Queue HealthSyncPackets { get; set; } = new(50); @@ -66,26 +67,44 @@ protected void Update() { if (isServer) { - int healthPackets = DamagePackets.Count; - for (int i = 0; i < healthPackets; i++) + int damagePackets = DamagePackets.Count; + for (int i = 0; i < damagePackets; i++) { - DamagePacket healthPacket = DamagePackets.Dequeue(); - healthPacket.NetId = player.NetId; + DamagePacket damagePacket = DamagePackets.Dequeue(); + damagePacket.NetId = player.NetId; Writer.Reset(); - Server.SendDataToAll(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered); + Server.SendDataToAll(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered); + } + int armorDamagePackets = ArmorDamagePackets.Count; + for (int i = 0; i < armorDamagePackets; i++) + { + ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue(); + armorDamagePacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered); } } else { - int healthPackets = DamagePackets.Count; - for (int i = 0; i < healthPackets; i++) + int damagePackets = DamagePackets.Count; + for (int i = 0; i < damagePackets; i++) + { + DamagePacket damagePacket = DamagePackets.Dequeue(); + damagePacket.NetId = player.NetId; + + Writer.Reset(); + Client.SendData(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered); + } + int armorDamagePackets = ArmorDamagePackets.Count; + for (int i = 0; i < armorDamagePackets; i++) { - DamagePacket healthPacket = DamagePackets.Dequeue(); - healthPacket.NetId = player.NetId; + ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue(); + armorDamagePacket.NetId = player.NetId; Writer.Reset(); - Client.SendData(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered); + Client.SendData(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered); } } } diff --git a/Fika.Core/Coop/PacketHandlers/PacketReceiver.cs b/Fika.Core/Coop/PacketHandlers/PacketReceiver.cs index 2a89b9f1..9cdf5ab8 100644 --- a/Fika.Core/Coop/PacketHandlers/PacketReceiver.cs +++ b/Fika.Core/Coop/PacketHandlers/PacketReceiver.cs @@ -19,6 +19,7 @@ public class PacketReceiver : MonoBehaviour public PlayerStatePacket NewState { get; set; } public Queue FirearmPackets { get; private set; } = new(50); public Queue DamagePackets { get; private set; } = new(50); + public Queue ArmorDamagePackets { get; private set; } = new(50); public Queue InventoryPackets { get; private set; } = new(50); public Queue CommonPlayerPackets { get; private set; } = new(50); public Queue HealthSyncPackets { get; private set; } = new(50); @@ -99,13 +100,22 @@ protected void Update() player.HandleWeaponPacket(FirearmPackets.Dequeue()); } } - int healthPackets = DamagePackets.Count; - if (healthPackets > 0) + int damagePackets = DamagePackets.Count; + if (damagePackets > 0) { - for (int i = 0; i < healthPackets; i++) + for (int i = 0; i < damagePackets; i++) { - DamagePacket packet = DamagePackets.Dequeue(); - player.HandleDamagePacket(ref packet); + DamagePacket damagePacket = DamagePackets.Dequeue(); + player.HandleDamagePacket(ref damagePacket); + } + } + int armorDamagePackets = ArmorDamagePackets.Count; + if (armorDamagePackets > 0) + { + for (int i = 0; i < armorDamagePackets; i++) + { + ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue(); + player.HandleArmorDamagePacket(ref armorDamagePacket); } } int inventoryPackets = InventoryPackets.Count; diff --git a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs index 5f9f9b15..d02c8a51 100644 --- a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs @@ -29,6 +29,7 @@ public class ServerPacketSender : MonoBehaviour, IPacketSender public NetDataWriter Writer { get; set; } = new(); public Queue FirearmPackets { get; set; } = new(50); public Queue DamagePackets { get; set; } = new(50); + public Queue ArmorDamagePackets { get; set; } = new(50); public Queue InventoryPackets { get; set; } = new(50); public Queue CommonPlayerPackets { get; set; } = new(50); public Queue HealthSyncPackets { get; set; } = new(50); @@ -99,16 +100,28 @@ protected void Update() Server.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered); } } - int healthPackets = DamagePackets.Count; - if (healthPackets > 0) + int damagePackets = DamagePackets.Count; + if (damagePackets > 0) { - for (int i = 0; i < healthPackets; i++) + for (int i = 0; i < damagePackets; i++) { - DamagePacket healthPacket = DamagePackets.Dequeue(); - healthPacket.NetId = player.NetId; + DamagePacket damagePacket = DamagePackets.Dequeue(); + damagePacket.NetId = player.NetId; Writer.Reset(); - Server.SendDataToAll(Writer, ref healthPacket, DeliveryMethod.ReliableOrdered); + Server.SendDataToAll(Writer, ref damagePacket, DeliveryMethod.ReliableOrdered); + } + } + int armorDamagePackets = ArmorDamagePackets.Count; + if (armorDamagePackets > 0) + { + for (int i = 0; i < armorDamagePackets; i++) + { + ArmorDamagePacket armorDamagePacket = ArmorDamagePackets.Dequeue(); + armorDamagePacket.NetId = player.NetId; + + Writer.Reset(); + Server.SendDataToAll(Writer, ref armorDamagePacket, DeliveryMethod.ReliableOrdered); } } int inventoryPackets = InventoryPackets.Count; diff --git a/Fika.Core/Coop/Players/CoopBot.cs b/Fika.Core/Coop/Players/CoopBot.cs index 0fd05256..d22d02de 100644 --- a/Fika.Core/Coop/Players/CoopBot.cs +++ b/Fika.Core/Coop/Players/CoopBot.cs @@ -94,14 +94,6 @@ public override void CreateMovementContext() MovementContext = BotMovementContext.Create(this, new Func(GetBodyAnimatorCommon), new Func(GetCharacterControllerCommon), movement_MASK); } - /*public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, float absorbed) - { - if (damageInfo.Player != null && damageInfo.Player.iPlayer is ObservedCoopPlayer) - return; - - base.ApplyDamageInfo(damageInfo, bodyPartType, colliderType, absorbed); - }*/ - public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct389 shotId) { if (damageInfo.Player != null && damageInfo.Player.iPlayer is ObservedCoopPlayer) @@ -136,6 +128,12 @@ public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy ApplyDamageInfo(damageInfo, bodyPartType, colliderType, 0f); ShotReactions(damageInfo, bodyPartType); ReceiveDamage(damageInfo.Damage, bodyPartType, damageInfo.DamageType, num, hitInfo.Material); + + if (list != null) + { + QueueArmorDamagePackets([.. list]); + } + return hitInfo; } diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index dde59a24..66a26d80 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -2,6 +2,7 @@ using Comfort.Common; using EFT; +using EFT.Ballistics; using EFT.Communications; using EFT.GlobalEvents; using EFT.HealthSystem; @@ -26,6 +27,7 @@ using System.Threading.Tasks; using UnityEngine; using static Fika.Core.Networking.FikaSerialization; +using static UnityEngine.SendMouseEvents; namespace Fika.Core.Coop.Players { @@ -219,17 +221,42 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct389 shotId) { - if (damageInfo.DamageType is EDamageType.Sniper or EDamageType.Landmine) + if (damageInfo.DamageType is not EDamageType.Sniper or EDamageType.Landmine) { - return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId); + return null; + } + + if (damageInfo.Player != null && damageInfo.Player.iPlayer is not CoopBot) + { + return null; } - if (damageInfo.Player != null && damageInfo.Player.iPlayer is CoopBot) + ActiveHealthController activeHealthController = ActiveHealthController; + if (activeHealthController != null && !activeHealthController.IsAlive) + { + return null; + } + bool flag = !string.IsNullOrEmpty(damageInfo.DeflectedBy); + float damage = damageInfo.Damage; + List list = ProceedDamageThroughArmor(ref damageInfo, colliderType, armorPlateCollider, true); + MaterialType materialType = flag ? MaterialType.HelmetRicochet : ((list == null || list.Count < 1) ? MaterialType.Body : list[0].Material); + GClass1688 gclass = new() + { + PoV = PointOfView, + Penetrated = string.IsNullOrEmpty(damageInfo.BlockedBy) || string.IsNullOrEmpty(damageInfo.DeflectedBy), + Material = materialType + }; + ApplyDamageInfo(damageInfo, bodyPartType, colliderType, 0f); + ShotReactions(damageInfo, bodyPartType); + float num = damage - damageInfo.Damage; + ReceiveDamage(damageInfo.Damage, bodyPartType, damageInfo.DamageType, num, gclass.Material); + + if (list != null) { - return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId); + QueueArmorDamagePackets([.. list]); } - return null; + return gclass; } public override void Proceed(bool withNetwork, Callback callback, bool scheduled = true) @@ -1095,18 +1122,6 @@ public virtual void HandleInventoryPacket(in InventoryPacket packet) FikaPlugin.Instance.FikaLogger.LogError("Split: Item was null"); } } - - /*// Fix for folding not replicating - if (result.Value is GClass2858 foldOperation) - { - if (HandsController is CoopObservedFirearmController observedFirearmController) - { - if (observedFirearmController.Weapon != null && observedFirearmController.Weapon.Foldable != null) - { - observedFirearmController.InitiateOperation().Start(foldOperation, null); - } - } - }*/ } catch (Exception exception) { @@ -1269,6 +1284,28 @@ public virtual void HandleWeaponPacket(in WeaponPacket packet) } } + public void QueueArmorDamagePackets(ArmorComponent[] armorComponents) + { + int amount = armorComponents.Length; + if (amount > 0) + { + string[] ids = new string[amount]; + float[] durabilities = new float[amount]; + + for (int i = 0; i < amount; i++) + { + ids[i] = armorComponents[i].Item.Id; + durabilities[i] = armorComponents[i].Repairable.Durability; + } + + PacketSender.ArmorDamagePackets.Enqueue(new() + { + ItemIds = ids, + Durabilities = durabilities, + }); + } + } + public virtual void HandleDamagePacket(ref DamagePacket packet) { DamageInfo damageInfo = new() @@ -1308,7 +1345,42 @@ public virtual void HandleDamagePacket(ref DamagePacket packet) } } - ClientApplyShot(damageInfo, packet.BodyPartType, packet.ColliderType, packet.ArmorPlateCollider); + ShotReactions(damageInfo, packet.BodyPartType); + ReceiveDamage(damageInfo.Damage, packet.BodyPartType, damageInfo.DamageType, packet.Absorbed, packet.Material); + ClientApplyDamageInfo(damageInfo, packet.BodyPartType, packet.ColliderType, packet.Absorbed); + //ClientApplyShot(damageInfo, packet.BodyPartType, packet.ColliderType, packet.ArmorPlateCollider); + } + + public void HandleArmorDamagePacket(ref ArmorDamagePacket packet) + { + for (int i = 0; i < packet.ItemIds.Length; i++) + { + _preAllocatedArmorComponents.Clear(); + Inventory.GetPutOnArmorsNonAlloc(_preAllocatedArmorComponents); + foreach (ArmorComponent armorComponent in _preAllocatedArmorComponents) + { + if (armorComponent.Item.Id == packet.ItemIds[i]) + { + armorComponent.Repairable.Durability = packet.Durabilities[i]; + armorComponent.Buff.TryDisableComponent(armorComponent.Repairable.Durability); + armorComponent.Item.RaiseRefreshEvent(false, false); + return; + } + } + GStruct416 gstruct = Singleton.Instance.FindItemById(packet.ItemIds[i]); + if (gstruct.Failed) + { + FikaPlugin.Instance.FikaLogger.LogError("HandleArmorDamagePacket: " + gstruct.Error); + return; + } + ArmorComponent itemComponent = gstruct.Value.GetItemComponent(); + if (itemComponent != null) + { + itemComponent.Repairable.Durability = packet.Durabilities[i]; + itemComponent.Buff.TryDisableComponent(itemComponent.Repairable.Durability); + itemComponent.Item.RaiseRefreshEvent(false, false); + } + } } public virtual void SetupDogTag() diff --git a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs index 35bda856..53b53229 100644 --- a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs +++ b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs @@ -7,6 +7,7 @@ using EFT.HealthSystem; using EFT.Interactive; using EFT.InventoryLogic; +using EFT.UI; using EFT.Vaulting; using Fika.Core.Coop.Custom; using Fika.Core.Coop.Factories; @@ -241,9 +242,8 @@ public override void ManageAggressor(DamageInfo damageInfo, EBodyPart bodyPart, } bool flag = damageInfo.DidBodyDamage / HealthController.GetBodyPartHealth(bodyPart, false).Maximum >= 0.6f && HealthController.FindExistingEffect(bodyPart) != null; - player.StatisticsManager.OnEnemyDamage( - damageInfo, bodyPart, ProfileId, Side, Profile.Info.Settings.Role, GroupId, - HealthController.GetBodyPartHealth(EBodyPart.Common, false).Maximum, flag, + player.StatisticsManager.OnEnemyDamage(damageInfo, bodyPart, ProfileId, Side, Profile.Info.Settings.Role, + GroupId, HealthController.GetBodyPartHealth(EBodyPart.Common, false).Maximum, flag, Vector3.Distance(player.Transform.position, Transform.position), CurrentHour, Inventory.EquippedInSlotsTemplateIds, HealthController.BodyPartEffects, TriggerZones); } @@ -299,33 +299,6 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy LastDamageType = damageInfo.DamageType; } - /*public override void ShotReactions(DamageInfo shot, EBodyPart bodyPart) - { - Vector3 normalized = shot.Direction.normalized; - if (PointOfView == EPointOfView.ThirdPerson) - { - turnOffFbbikAt = Time.time + 0.6f; - _fbbik.solver.Quick = false; - BodyPartCollider bodyPartCollider; - if ((bodyPartCollider = shot.HittedBallisticCollider as BodyPartCollider) != null) - { - HitReaction.Hit(bodyPartCollider.BodyPartColliderType, bodyPartCollider.BodyPartType, normalized, shot.HitPoint, false); - } - } - if (shot.Weapon is KnifeClass knifeClass) - { - KnifeComponent itemComponent = knifeClass.GetItemComponent(); - Vector3 normalized2 = (shot.Player.iPlayer.Transform.position - Transform.position).normalized; - Vector3 vector = Vector3.Cross(normalized2, Vector3.up); - float y = normalized.y; - float num = Vector3.Dot(vector, normalized); - float num2 = 1f - Mathf.Abs(Vector3.Dot(normalized2, normalized)); - num2 = ((bodyPart == EBodyPart.Head) ? num2 : Mathf.Sqrt(num2)); - Rotation += new Vector2(-num, -y).normalized * itemComponent.Template.AppliedTrunkRotation.Random(false) * num2; - ProceduralWeaponAnimation.ForceReact.AddForce(new Vector3(-y, num, 0f).normalized, num2, 1f, itemComponent.Template.AppliedHeadRotation.Random(false)); - } - }*/ - public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct389 shotId) { if (damageInfo.DamageType == EDamageType.Sniper && MatchmakerAcceptPatches.IsServer) @@ -363,7 +336,7 @@ public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy // There should never be other instances than CoopPlayer or its derived types CoopPlayer player = (CoopPlayer)damageInfo.Player.iPlayer; - if (player.IsYourPlayer) + if (player.IsYourPlayer || player.IsAI) { if (HealthController != null && !HealthController.IsAlive) { @@ -386,19 +359,12 @@ public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy damageInfo.DidArmorDamage = num; } damageInfo.DidBodyDamage = damageInfo.Damage; - //ApplyDamageInfo(damageInfo, bodyPartType, colliderType, 0f); ShotReactions(damageInfo, bodyPartType); ReceiveDamage(damageInfo.Damage, bodyPartType, damageInfo.DamageType, num, hitInfo.Material); - if (damageInfo.HittedBallisticCollider != null) - { - BodyPartCollider bodyPartCollider = (BodyPartCollider)damageInfo.HittedBallisticCollider; - colliderType = bodyPartCollider.BodyPartColliderType; - } - PacketSender.DamagePackets.Enqueue(new() { - Damage = damage, + Damage = damageInfo.Damage, DamageType = damageInfo.DamageType, BodyPartType = bodyPartType, ColliderType = colliderType, @@ -412,66 +378,15 @@ public override GClass1688 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy DeflectedBy = damageInfo.DeflectedBy, SourceId = damageInfo.SourceId, ArmorDamage = damageInfo.ArmorDamage, - ProfileId = damageInfo.Player.iPlayer.ProfileId - }); - - // Run this to get weapon skill - ManageAggressor(damageInfo, bodyPartType, colliderType); - - return hitInfo; - } - else if (player.IsAI) - { - if (HealthController != null && !HealthController.IsAlive) - { - return null; - } - - bool flag = !string.IsNullOrEmpty(damageInfo.DeflectedBy); - float damage = damageInfo.Damage; - List list = ProceedDamageThroughArmor(ref damageInfo, colliderType, armorPlateCollider, true); - MaterialType materialType = flag ? MaterialType.HelmetRicochet : ((list == null || list.Count < 1) ? MaterialType.Body : list[0].Material); - GClass1688 hitInfo = new() - { - PoV = PointOfView, - Penetrated = string.IsNullOrEmpty(damageInfo.BlockedBy) || string.IsNullOrEmpty(damageInfo.DeflectedBy), + ProfileId = damageInfo.Player.iPlayer.ProfileId, Material = materialType - }; - float num = damage - damageInfo.Damage; - if (num > 0) - { - damageInfo.DidArmorDamage = num; - } - damageInfo.DidBodyDamage = damageInfo.Damage; - //ApplyDamageInfo(damageInfo, bodyPartType, colliderType, 0f); - ShotReactions(damageInfo, bodyPartType); - ReceiveDamage(damageInfo.Damage, bodyPartType, damageInfo.DamageType, num, hitInfo.Material); + }); - if (damageInfo.HittedBallisticCollider != null) + if (list != null) { - BodyPartCollider bodyPartCollider = (BodyPartCollider)damageInfo.HittedBallisticCollider; - colliderType = bodyPartCollider.BodyPartColliderType; + QueueArmorDamagePackets([.. list]); } - PacketSender.DamagePackets.Enqueue(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 - }); - // Run this to get weapon skill ManageAggressor(damageInfo, bodyPartType, colliderType); diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index 39c7f15b..d15b8345 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -73,6 +73,7 @@ public void Init() packetProcessor.SubscribeNetSerializable(OnGameTimerPacketReceived); packetProcessor.SubscribeNetSerializable(OnFirearmPacketReceived); packetProcessor.SubscribeNetSerializable(OnDamagePacketReceived); + packetProcessor.SubscribeNetSerializable(OnArmorDamagePacketReceived); packetProcessor.SubscribeNetSerializable(OnInventoryPacketReceived); packetProcessor.SubscribeNetSerializable(OnCommonPlayerPacketReceived); packetProcessor.SubscribeNetSerializable(OnAllCharacterRequestPacketReceived); @@ -655,6 +656,14 @@ private void OnDamagePacketReceived(DamagePacket packet) } } + private void OnArmorDamagePacketReceived(ArmorDamagePacket packet) + { + if (Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) + { + playerToApply.PacketReceiver.ArmorDamagePackets?.Enqueue(packet); + } + } + private void OnFirearmPacketReceived(WeaponPacket packet) { if (Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index 6133d8c5..f7ac8b0f 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -82,6 +82,7 @@ public async Task Init() packetProcessor.SubscribeNetSerializable(OnGameTimerPacketReceived); packetProcessor.SubscribeNetSerializable(OnFirearmPacketReceived); packetProcessor.SubscribeNetSerializable(OnDamagePacketReceived); + packetProcessor.SubscribeNetSerializable(OnArmorDamagePacketReceived); packetProcessor.SubscribeNetSerializable(OnInventoryPacketReceived); packetProcessor.SubscribeNetSerializable(OnCommonPlayerPacketReceived); packetProcessor.SubscribeNetSerializable(OnAllCharacterRequestPacketReceived); @@ -641,6 +642,17 @@ private void OnDamagePacketReceived(DamagePacket packet, NetPeer peer) SendDataToAll(_dataWriter, ref packet, DeliveryMethod.ReliableOrdered, peer); } + private void OnArmorDamagePacketReceived(ArmorDamagePacket packet, NetPeer peer) + { + if (Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) + { + playerToApply.PacketReceiver.ArmorDamagePackets?.Enqueue(packet); + } + + _dataWriter.Reset(); + SendDataToAll(_dataWriter, ref packet, DeliveryMethod.ReliableOrdered, peer); + } + private void OnFirearmPacketReceived(WeaponPacket packet, NetPeer peer) { if (Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) diff --git a/Fika.Core/Networking/Packets/Player/ArmorDamagePacket.cs b/Fika.Core/Networking/Packets/Player/ArmorDamagePacket.cs new file mode 100644 index 00000000..3e7de4ff --- /dev/null +++ b/Fika.Core/Networking/Packets/Player/ArmorDamagePacket.cs @@ -0,0 +1,25 @@ +using LiteNetLib.Utils; + +namespace Fika.Core.Networking +{ + public struct ArmorDamagePacket(int netId) : INetSerializable + { + public int NetId = netId; + public string[] ItemIds; + public float[] Durabilities; + + public void Deserialize(NetDataReader reader) + { + NetId = reader.GetInt(); + ItemIds = reader.GetStringArray(); + Durabilities = reader.GetFloatArray(); + } + + public void Serialize(NetDataWriter writer) + { + writer.Put(NetId); + writer.PutArray(ItemIds); + writer.PutArray(Durabilities); + } + } +} diff --git a/Fika.Core/Networking/Packets/Player/DamagePacket.cs b/Fika.Core/Networking/Packets/Player/DamagePacket.cs index e21a31c8..91e011b4 100644 --- a/Fika.Core/Networking/Packets/Player/DamagePacket.cs +++ b/Fika.Core/Networking/Packets/Player/DamagePacket.cs @@ -1,4 +1,5 @@ using EFT; +using EFT.Ballistics; using LiteNetLib.Utils; using UnityEngine; @@ -24,6 +25,7 @@ public struct DamagePacket(int netId) : INetSerializable public int FragmentIndex; public float ArmorDamage = 0f; public string ProfileId; + public MaterialType Material = 0; public void Deserialize(NetDataReader reader) { @@ -45,6 +47,7 @@ public void Deserialize(NetDataReader reader) FragmentIndex = reader.GetInt(); ArmorDamage = reader.GetFloat(); ProfileId = reader.GetString(); + Material = (MaterialType)reader.GetInt(); } public void Serialize(NetDataWriter writer) @@ -67,6 +70,7 @@ public void Serialize(NetDataWriter writer) writer.Put(FragmentIndex); writer.Put(ArmorDamage); writer.Put(ProfileId); + writer.Put((int)Material); } } }