From 441b5611c011b2ee6a927e455358b007a89a1fee Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Thu, 16 May 2024 18:46:37 +0200 Subject: [PATCH] Fix minefields and sniper zones not causing damage --- .../Coop/PacketHandlers/BotPacketSender.cs | 8 +- .../Coop/PacketHandlers/ClientPacketSender.cs | 8 +- .../Coop/PacketHandlers/IPacketSender.cs | 2 +- .../PacketHandlers/ObservedPacketSender.cs | 14 ++-- .../Coop/PacketHandlers/ServerPacketSender.cs | 8 +- .../Minefield/Minefield_method_2_Patch.cs | 78 ++++++++++++++++++- Fika.Core/Coop/Players/CoopPlayer.cs | 10 ++- Fika.Core/Coop/Players/ObservedCoopPlayer.cs | 58 +++++++++++++- Fika.Core/Networking/FikaSerialization.cs | 3 + 9 files changed, 162 insertions(+), 27 deletions(-) diff --git a/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs b/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs index c2096d87..cba9122a 100644 --- a/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs @@ -18,7 +18,7 @@ public class BotPacketSender : MonoBehaviour, IPacketSender public FikaClient Client { get; set; } public NetDataWriter Writer { get; set; } = new(); public Queue FirearmPackets { get; set; } = new(50); - public Queue HealthPackets { get; set; } = new(50); + public Queue DamagePackets { 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); @@ -62,12 +62,12 @@ private void Update() Server?.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered); } } - int healthPackets = HealthPackets.Count; + int healthPackets = DamagePackets.Count; if (healthPackets > 0) { for (int i = 0; i < healthPackets; i++) { - DamagePacket healthPacket = HealthPackets.Dequeue(); + DamagePacket healthPacket = DamagePackets.Dequeue(); healthPacket.NetId = player.NetId; Writer?.Reset(); @@ -116,7 +116,7 @@ public void DestroyThis() { Writer = null; FirearmPackets.Clear(); - HealthPackets.Clear(); + DamagePackets.Clear(); InventoryPackets.Clear(); CommonPlayerPackets.Clear(); HealthSyncPackets.Clear(); diff --git a/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs index 3421c752..3a7aeca6 100644 --- a/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs @@ -23,7 +23,7 @@ public class ClientPacketSender : MonoBehaviour, IPacketSender public FikaClient Client { get; set; } public NetDataWriter Writer { get; set; } = new(); public Queue FirearmPackets { get; set; } = new(50); - public Queue HealthPackets { get; set; } = new(50); + public Queue DamagePackets { 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); @@ -74,12 +74,12 @@ private void Update() Client?.SendData(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered); } } - int healthPackets = HealthPackets.Count; + int healthPackets = DamagePackets.Count; if (healthPackets > 0) { for (int i = 0; i < healthPackets; i++) { - DamagePacket healthPacket = HealthPackets.Dequeue(); + DamagePacket healthPacket = DamagePackets.Dequeue(); healthPacket.NetId = player.NetId; Writer?.Reset(); @@ -199,7 +199,7 @@ public void DestroyThis() { Writer = null; FirearmPackets.Clear(); - HealthPackets.Clear(); + DamagePackets.Clear(); InventoryPackets.Clear(); CommonPlayerPackets.Clear(); HealthSyncPackets.Clear(); diff --git a/Fika.Core/Coop/PacketHandlers/IPacketSender.cs b/Fika.Core/Coop/PacketHandlers/IPacketSender.cs index 1620f1be..bf14e663 100644 --- a/Fika.Core/Coop/PacketHandlers/IPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/IPacketSender.cs @@ -12,7 +12,7 @@ public interface IPacketSender public FikaClient Client { get; set; } public NetDataWriter Writer { get; set; } public Queue FirearmPackets { get; set; } - public Queue HealthPackets { get; set; } + public Queue DamagePackets { 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 6f4a7b2a..95f641c9 100644 --- a/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs @@ -19,7 +19,7 @@ public class ObservedPacketSender : MonoBehaviour, IPacketSender public FikaClient Client { get; set; } public NetDataWriter Writer { get; set; } = new(); public Queue FirearmPackets { get; set; } = new(50); - public Queue HealthPackets { get; set; } = new(50); + public Queue DamagePackets { 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); @@ -45,14 +45,14 @@ private void Update() return; } - if (HealthPackets.Count > 0) + if (DamagePackets.Count > 0) { if (isServer) { - int healthPackets = HealthPackets.Count; + int healthPackets = DamagePackets.Count; for (int i = 0; i < healthPackets; i++) { - DamagePacket healthPacket = HealthPackets.Dequeue(); + DamagePacket healthPacket = DamagePackets.Dequeue(); healthPacket.NetId = player.NetId; Writer.Reset(); @@ -61,10 +61,10 @@ private void Update() } else { - int healthPackets = HealthPackets.Count; + int healthPackets = DamagePackets.Count; for (int i = 0; i < healthPackets; i++) { - DamagePacket healthPacket = HealthPackets.Dequeue(); + DamagePacket healthPacket = DamagePackets.Dequeue(); healthPacket.NetId = player.NetId; Writer.Reset(); @@ -78,7 +78,7 @@ public void DestroyThis() { Writer = null; FirearmPackets.Clear(); - HealthPackets.Clear(); + DamagePackets.Clear(); InventoryPackets.Clear(); CommonPlayerPackets.Clear(); HealthSyncPackets.Clear(); diff --git a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs index 3efd2cbc..142cecdd 100644 --- a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs @@ -26,7 +26,7 @@ public class ServerPacketSender : MonoBehaviour, IPacketSender public FikaClient Client { get; set; } public NetDataWriter Writer { get; set; } = new(); public Queue FirearmPackets { get; set; } = new(50); - public Queue HealthPackets { get; set; } = new(50); + public Queue DamagePackets { 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); @@ -81,12 +81,12 @@ private void Update() Server?.SendDataToAll(Writer, ref firearmPacket, DeliveryMethod.ReliableOrdered); } } - int healthPackets = HealthPackets.Count; + int healthPackets = DamagePackets.Count; if (healthPackets > 0) { for (int i = 0; i < healthPackets; i++) { - DamagePacket healthPacket = HealthPackets.Dequeue(); + DamagePacket healthPacket = DamagePackets.Dequeue(); healthPacket.NetId = player.NetId; Writer?.Reset(); @@ -195,7 +195,7 @@ public void DestroyThis() { Writer = null; FirearmPackets.Clear(); - HealthPackets.Clear(); + DamagePackets.Clear(); InventoryPackets.Clear(); CommonPlayerPackets.Clear(); HealthSyncPackets.Clear(); 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 4df8782f..c249eb08 100644 --- a/Fika.Core/Coop/Patches/Minefield/Minefield_method_2_Patch.cs +++ b/Fika.Core/Coop/Patches/Minefield/Minefield_method_2_Patch.cs @@ -1,8 +1,16 @@ using Aki.Reflection.Patching; +using Comfort.Common; using EFT; using EFT.Interactive; +using Fika.Core.Coop.Matchmaker; using Fika.Core.Coop.Players; +using System; +using System.Collections.Generic; +using System.Linq; using System.Reflection; +using UnityEngine; +using static UnityEngine.ParticleSystem; +using static UnityEngine.Tilemaps.Tile; namespace Fika.Core.Coop.Patches { @@ -17,13 +25,81 @@ protected override MethodBase GetTargetMethod() } [PatchPrefix] - public static bool Prefix(IPlayer player) + public static bool Prefix(IPlayer player, bool first, List ___TargetedPlayers, + List ___NotTargetedPlayers, float ____collateralContusionRange, float ____collateralDamageRange, + float ____firstExplosionDamage, float ____secondExplosionDamage, Minefield __instance) { if (player is ObservedCoopPlayer) { + if (MatchmakerAcceptPatches.IsServer) + { + Vector3 position = player.Position; + foreach (IPlayer player2 in ___TargetedPlayers.Concat(___NotTargetedPlayers).ToList()) + { + DoReplicatedMineDamage(player2, Vector3.Distance(position, player2.Position), first, + player != player2, ____collateralContusionRange, ____collateralDamageRange, + ____firstExplosionDamage, ____secondExplosionDamage, __instance); + } + } return false; } return true; } + + private static void DoReplicatedMineDamage(IPlayer player, float distance, bool first, bool isCollateral, + float collateralContusionRange, float collateralDamageRange, float firstExplosionDamage, + float secondExplosionDamage, Minefield minefield) + { + if (isCollateral && distance > collateralContusionRange) + { + return; + } + + CoopPlayer coopPlayer = (CoopPlayer)Singleton.Instance.GetAlivePlayerByProfileID(player.ProfileId); + if (isCollateral && distance > collateralDamageRange) + { + return; + } + + if (coopPlayer != null) + { + float num2 = 1f - distance / collateralDamageRange; + IEnumerable enumerable = isCollateral ? player.PlayerBones.BodyPartColliders.Where(new Func(minefield.method_4)) + : player.PlayerBones.BodyPartColliders.Where(new Func(minefield.method_5)); + + enumerable = enumerable.DistinctBy(new Func(Minefield.Class2286.class2286_0.method_0)).ToArray(); + enumerable = enumerable.Randomize(); + + int num3 = ((isCollateral || first) ? UnityEngine.Random.Range(2, enumerable.Count()) : int.MaxValue); + float num4 = (isCollateral || first) ? firstExplosionDamage : secondExplosionDamage; + int num5 = 0; + + foreach (BodyPartCollider bodyPartCollider in enumerable) + { + 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 + } + }); + if (++num5 >= num3) + { + break; + } + } + } + else + { + FikaPlugin.Instance.FikaLogger.LogError($"DoReplicatedMineDamage: Could not find player with ProfileId: {player.ProfileId}, Nickname: {player.Profile.Nickname}!"); + } + } } } diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index 77e6f4f2..75aa5fa6 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -230,7 +230,12 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct390 shotId) { - if (damageInfo.Player != null & damageInfo.Player.iPlayer is CoopBot) + if (damageInfo.DamageType is EDamageType.Sniper or EDamageType.Landmine) + { + return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId); + } + + if (damageInfo.Player != null && damageInfo.Player.iPlayer is CoopBot) { return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId); } @@ -1333,7 +1338,8 @@ public virtual void HandleDamagePacket(DamagePacket packet) PenetrationPower = packet.DamageInfo.PenetrationPower, BlockedBy = packet.DamageInfo.BlockedBy, DeflectedBy = packet.DamageInfo.DeflectedBy, - SourceId = packet.DamageInfo.SourceId + SourceId = packet.DamageInfo.SourceId, + ArmorDamage = packet.DamageInfo.ArmorDamage }; if (!string.IsNullOrEmpty(packet.DamageInfo.ProfileId)) diff --git a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs index 22f9a6c5..4d60ee99 100644 --- a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs +++ b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs @@ -1,5 +1,6 @@ // © 2024 Lacyway All Rights Reserved +using Aki.SinglePlayer.Patches.MainMenu; using Comfort.Common; using EFT; using EFT.Ballistics; @@ -11,6 +12,7 @@ using Fika.Core.Coop.Custom; using Fika.Core.Coop.Factories; using Fika.Core.Coop.GameMode; +using Fika.Core.Coop.Matchmaker; using Fika.Core.Coop.ObservedClasses; using Fika.Core.Coop.PacketHandlers; using Fika.Core.Networking; @@ -278,6 +280,31 @@ public override bool ShouldVocalizeDeath(EBodyPart bodyPart) public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, float absorbed) { + if (damageInfo.DamageType == EDamageType.Landmine && MatchmakerAcceptPatches.IsServer) + { + 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 + } + }); + + return; + } + if (damageInfo.Player == null) { return; @@ -324,11 +351,32 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct390 shotId) { - /*if (!IsObservedAI) + if (damageInfo.DamageType == EDamageType.Sniper && MatchmakerAcceptPatches.IsServer) { 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 + } + }); + return null; - }*/ + } if (damageInfo.Player != null) { @@ -374,7 +422,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy colliderType = bodyPartCollider.BodyPartColliderType; } - PacketSender?.HealthPackets?.Enqueue(new() + PacketSender?.DamagePackets?.Enqueue(new() { DamageInfo = new() { @@ -391,6 +439,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy BlockedBy = damageInfo.BlockedBy, DeflectedBy = damageInfo.DeflectedBy, SourceId = damageInfo.SourceId, + ArmorDamage = damageInfo.ArmorDamage, ProfileId = damageInfo.Player.iPlayer.ProfileId } }); @@ -433,7 +482,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy colliderType = bodyPartCollider.BodyPartColliderType; } - PacketSender?.HealthPackets?.Enqueue(new() + PacketSender?.DamagePackets?.Enqueue(new() { DamageInfo = new() { @@ -450,6 +499,7 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy BlockedBy = damageInfo.BlockedBy, DeflectedBy = damageInfo.DeflectedBy, SourceId = damageInfo.SourceId, + ArmorDamage = damageInfo.ArmorDamage, ProfileId = damageInfo.Player.iPlayer.ProfileId } }); diff --git a/Fika.Core/Networking/FikaSerialization.cs b/Fika.Core/Networking/FikaSerialization.cs index 709c3f49..846c70dc 100644 --- a/Fika.Core/Networking/FikaSerialization.cs +++ b/Fika.Core/Networking/FikaSerialization.cs @@ -651,6 +651,7 @@ public struct ApplyShotPacket() public string SourceId; public string AmmoId; public int FragmentIndex; + public float ArmorDamage = 0f; public string ProfileId; public static ApplyShotPacket Deserialize(NetDataReader reader) @@ -672,6 +673,7 @@ public static ApplyShotPacket Deserialize(NetDataReader reader) SourceId = reader.GetString(), AmmoId = reader.GetString(), FragmentIndex = reader.GetInt(), + ArmorDamage = reader.GetFloat(), ProfileId = reader.GetString() }; return packet; @@ -693,6 +695,7 @@ public static void Serialize(NetDataWriter writer, ApplyShotPacket packet) writer.Put(packet.SourceId); writer.Put(packet.AmmoId); writer.Put(packet.FragmentIndex); + writer.Put(packet.ArmorDamage); writer.Put(packet.ProfileId); } }