From ccca97335ff4c3db08c31c0352654cd67ce5f626 Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Sun, 4 Aug 2024 03:02:15 -0400 Subject: [PATCH 1/7] Untested --- .../Patches/Generic/IndividualFriendlyFire.cs | 61 +++++++------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs index 1100d39ca1..b610cbb75b 100644 --- a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs +++ b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs @@ -89,17 +89,18 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref // Return false, no custom friendly fire allowed, default to NW logic for FF. No point in processing if FF is enabled across the board. if (Server.FriendlyFire) - return HitboxIdentity.IsEnemy(attackerFootprint.Role, victimHub.roleManager.CurrentRole.RoleTypeId); + return false; - // always allow damage from Server.Host + // Always allow damage from Server.Host if (attackerFootprint.Hub == Server.Host.ReferenceHub) return true; // Only check friendlyFire if the FootPrint hasn't changed (Fix for Grenade not dealing damage because it's from a dead player) // TODO rework FriendlyFireRule to make it compatible with Footprint - if (!attackerFootprint.SameLife(new(attackerFootprint.Hub))) - return HitboxIdentity.IsEnemy(attackerFootprint.Role, victimHub.roleManager.CurrentRole.RoleTypeId); + if (!attackerFootprint.SameLife(new Footprint(attackerFootprint.Hub))) + return false; + // Check if attackerFootprint.Hub or victimHub is null and log debug information if (attackerFootprint.Hub is null || victimHub is null) { Log.Debug($"CheckFriendlyFirePlayerRules, Attacker hub null: {attackerFootprint.Hub is null}, Victim hub null: {victimHub is null}"); @@ -110,6 +111,7 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref { Player attacker = Player.Get(attackerFootprint.Hub); Player victim = Player.Get(victimHub); + if (attacker is null || victim is null) { Log.Debug($"CheckFriendlyFirePlayerRules, Attacker null: {attacker is null}, Victim null: {victim is null}"); @@ -124,45 +126,27 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref Log.Debug($"CheckFriendlyFirePlayerRules, Attacker role {attacker.Role} and Victim {victim.Role}"); - if (!string.IsNullOrEmpty(victim.UniqueRole)) + // Check victim's UniqueRole for custom FF multiplier + if (!string.IsNullOrEmpty(victim.UniqueRole) && + victim.CustomRoleFriendlyFireMultiplier.TryGetValue(victim.UniqueRole, out var victimPairedData) && + victimPairedData.TryGetValue(attacker.Role, out ffMultiplier)) { - // If 035 is being shot, then we need to check if we are an 035, then check if the attacker is allowed to attack us - if (victim.CustomRoleFriendlyFireMultiplier.Count > 0) - { - if (victim.CustomRoleFriendlyFireMultiplier.TryGetValue(victim.UniqueRole, out Dictionary pairedData)) - { - if (pairedData.ContainsKey(attacker.Role)) - { - ffMultiplier = pairedData[attacker.Role]; - return true; - } - } - } + return true; } - else if (!string.IsNullOrEmpty(attacker.UniqueRole)) + + // Check attacker's UniqueRole for custom FF multiplier + if (!string.IsNullOrEmpty(attacker.UniqueRole) && + attacker.CustomRoleFriendlyFireMultiplier.TryGetValue(attacker.UniqueRole, out var attackerPairedData) && + attackerPairedData.TryGetValue(victim.Role, out ffMultiplier)) { - // If 035 is attacking, whether to allow or disallow based on victim role. - if (attacker.CustomRoleFriendlyFireMultiplier.Count > 0) - { - if (attacker.CustomRoleFriendlyFireMultiplier.TryGetValue(attacker.UniqueRole, out Dictionary pairedData)) - { - if (pairedData.ContainsKey(victim.Role)) - { - ffMultiplier = pairedData[victim.Role]; - return true; - } - } - } + return true; } - // If we're SCP then we need to check if we can attack other SCP, or D-Class, etc. This is default FF logic without unique roles. - if (attacker.FriendlyFireMultiplier.Count > 0) + // Default FF logic for SCP or other roles without unique roles + if (!attacker.FriendlyFireMultiplier.IsEmpty() && + attacker.FriendlyFireMultiplier.TryGetValue(victim.Role, out ffMultiplier)) { - if (attacker.FriendlyFireMultiplier.TryGetValue(victim.Role, out float ffMulti)) - { - ffMultiplier = ffMulti; - return true; - } + return true; } } catch (Exception ex) @@ -170,7 +154,8 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref Log.Error($"CheckFriendlyFirePlayerRules failed to handle friendly fire because: {ex}"); } - return HitboxIdentity.IsEnemy(attackerFootprint.Role, victimHub.roleManager.CurrentRole.RoleTypeId); + // Default to NW logic + return false; } } From a25be5b4ab0a6384ff264ee019aa9c0251dd6e01 Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Sun, 4 Aug 2024 03:25:56 -0400 Subject: [PATCH 2/7] Testing alternative. --- .../Patches/Generic/IndividualFriendlyFire.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs index b610cbb75b..18d47060c1 100644 --- a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs +++ b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs @@ -211,7 +211,7 @@ private static IEnumerable Transpiler(IEnumerable instruction.Calls(PropertyGetter(typeof(AttackerDamageHandler), nameof(AttackerDamageHandler.Attacker)))) + offset; + instruction => instruction.LoadsField(Field(typeof(ReferenceHub), nameof(ReferenceHub.networkIdentity)))) + offset; LocalBuilder ffMulti = generator.DeclareLocal(typeof(float)); @@ -220,7 +220,7 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable instruction.opcode == OpCodes.Ret) + ffMultiplierIndexOffset; newInstructions.InsertRange( ffMultiplierIndex, - new CodeInstruction[] + new[] { // Do not run our custom logic, skip over. new(OpCodes.Br, normalProcessing), // AttackerDamageHandler.Damage = AttackerDamageHandler.Damage * ffMulti new CodeInstruction(OpCodes.Ldarg_0).WithLabels(uniqueFFMulti), - new(OpCodes.Ldloc, ffMulti.LocalIndex), + new (OpCodes.Ldloc, ffMulti.LocalIndex), new(OpCodes.Ldarg_0), new(OpCodes.Callvirt, PropertyGetter(typeof(AttackerDamageHandler), nameof(AttackerDamageHandler.Damage))), new(OpCodes.Mul), From d7427669a3d05005993086073e61eadd83c4a39c Mon Sep 17 00:00:00 2001 From: Yamato <66829532+louis1706@users.noreply.github.com> Date: Sun, 4 Aug 2024 12:35:09 +0200 Subject: [PATCH 3/7] Update EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs --- EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs index 18d47060c1..8151e8d092 100644 --- a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs +++ b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs @@ -261,7 +261,7 @@ private static IEnumerable Transpiler(IEnumerable Date: Wed, 7 Aug 2024 12:33:14 -0400 Subject: [PATCH 4/7] Option 1. --- .../Patches/Events/Map/ExplodingFlashGrenade.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/EXILED/Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs b/EXILED/Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs index 8042346854..92a6df5620 100644 --- a/EXILED/Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs +++ b/EXILED/Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs @@ -69,6 +69,12 @@ private static void ProcessEvent(FlashbangGrenade instance, float distance) continue; if (!ExiledEvents.Instance.Config.CanFlashbangsAffectThrower && instance.PreviousOwner.SameLife(new(referenceHub))) continue; + if (Server.FriendlyFire) + { + targetToAffect.Add(player); + continue; + } + if (!IndividualFriendlyFire.CheckFriendlyFirePlayer(instance.PreviousOwner, player.ReferenceHub) && !instance.PreviousOwner.SameLife(new(referenceHub))) continue; if (Physics.Linecast(instance.transform.position, player.CameraTransform.position, instance._blindingMask)) From 6d14558540cb7e56edb866f487a2890b26571d2b Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Wed, 7 Aug 2024 12:40:51 -0400 Subject: [PATCH 5/7] Option 2 --- .../Patches/Events/Map/ExplodingFlashGrenade.cs | 6 ------ .../Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs b/EXILED/Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs index 92a6df5620..8042346854 100644 --- a/EXILED/Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs +++ b/EXILED/Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs @@ -69,12 +69,6 @@ private static void ProcessEvent(FlashbangGrenade instance, float distance) continue; if (!ExiledEvents.Instance.Config.CanFlashbangsAffectThrower && instance.PreviousOwner.SameLife(new(referenceHub))) continue; - if (Server.FriendlyFire) - { - targetToAffect.Add(player); - continue; - } - if (!IndividualFriendlyFire.CheckFriendlyFirePlayer(instance.PreviousOwner, player.ReferenceHub) && !instance.PreviousOwner.SameLife(new(referenceHub))) continue; if (Physics.Linecast(instance.transform.position, player.CameraTransform.position, instance._blindingMask)) diff --git a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs index 8151e8d092..4ff244cc79 100644 --- a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs +++ b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs @@ -89,7 +89,7 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref // Return false, no custom friendly fire allowed, default to NW logic for FF. No point in processing if FF is enabled across the board. if (Server.FriendlyFire) - return false; + return HitboxIdentity.IsDamageable(attackerFootprint.Hub, victimHub); // Always allow damage from Server.Host if (attackerFootprint.Hub == Server.Host.ReferenceHub) @@ -155,7 +155,7 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref } // Default to NW logic - return false; + return HitboxIdentity.IsDamageable(attackerFootprint.Hub, victimHub); } } From 45de25208b7eb9215189d11c92e3ca77d39528b9 Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Thu, 8 Aug 2024 13:01:22 -0400 Subject: [PATCH 6/7] Option 2 updated. --- .../Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs index 4ff244cc79..9c6b8e581a 100644 --- a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs +++ b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs @@ -89,7 +89,7 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref // Return false, no custom friendly fire allowed, default to NW logic for FF. No point in processing if FF is enabled across the board. if (Server.FriendlyFire) - return HitboxIdentity.IsDamageable(attackerFootprint.Hub, victimHub); + return HitboxIdentity.IsDamageable(attackerFootprint.Role, victimHub.roleManager.CurrentRole.RoleTypeId); // Always allow damage from Server.Host if (attackerFootprint.Hub == Server.Host.ReferenceHub) @@ -155,7 +155,7 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref } // Default to NW logic - return HitboxIdentity.IsDamageable(attackerFootprint.Hub, victimHub); + return HitboxIdentity.IsDamageable(attackerFootprint.Role, victimHub.roleManager.CurrentRole.RoleTypeId); } } From 5e8ca7e38b5d362346ad14e056e16b73b684c463 Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Tue, 13 Aug 2024 18:41:28 -0400 Subject: [PATCH 7/7] Var removal. --- .../Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs index 9c6b8e581a..e30b2e58a1 100644 --- a/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs +++ b/EXILED/Exiled.Events/Patches/Generic/IndividualFriendlyFire.cs @@ -128,7 +128,7 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref // Check victim's UniqueRole for custom FF multiplier if (!string.IsNullOrEmpty(victim.UniqueRole) && - victim.CustomRoleFriendlyFireMultiplier.TryGetValue(victim.UniqueRole, out var victimPairedData) && + victim.CustomRoleFriendlyFireMultiplier.TryGetValue(victim.UniqueRole, out Dictionary victimPairedData) && victimPairedData.TryGetValue(attacker.Role, out ffMultiplier)) { return true; @@ -136,7 +136,7 @@ public static bool CheckFriendlyFirePlayerRules(Footprint attackerFootprint, Ref // Check attacker's UniqueRole for custom FF multiplier if (!string.IsNullOrEmpty(attacker.UniqueRole) && - attacker.CustomRoleFriendlyFireMultiplier.TryGetValue(attacker.UniqueRole, out var attackerPairedData) && + attacker.CustomRoleFriendlyFireMultiplier.TryGetValue(attacker.UniqueRole, out Dictionary attackerPairedData) && attackerPairedData.TryGetValue(victim.Role, out ffMultiplier)) { return true;