From ebac61e0a472d019745a72b9a81eb20c9e507410 Mon Sep 17 00:00:00 2001 From: dwesterwick Date: Fri, 16 Aug 2024 17:35:35 +0000 Subject: [PATCH] IsEnemyPatch Improvements (Part 2) (!157) Made the following changes to !156: * Replaced `IsNullOrEmpty()` EFT checks with basic null checks * Replaced `Any()` with new helper method to check if bots exist in collections Reviewed-on: https://dev.sp-tarkov.com/SPT/Modules/pulls/157 Co-authored-by: dwesterwick Co-committed-by: dwesterwick --- project/SPT.Custom/CustomAI/AiHelpers.cs | 26 +++++++++++++++++++++- project/SPT.Custom/Patches/IsEnemyPatch.cs | 18 +++++++-------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/project/SPT.Custom/CustomAI/AiHelpers.cs b/project/SPT.Custom/CustomAI/AiHelpers.cs index 60153c8..fc99a2b 100644 --- a/project/SPT.Custom/CustomAI/AiHelpers.cs +++ b/project/SPT.Custom/CustomAI/AiHelpers.cs @@ -47,7 +47,7 @@ namespace SPT.Custom.CustomAI public static List GetAllMembers(this BotsGroup group) { List members = new List(); - + if (group != null) { for (int m = 0; m < group.MembersCount; m++) @@ -58,5 +58,29 @@ namespace SPT.Custom.CustomAI return members; } + + /// + /// Returns true if the player is found in the collection by searching for matching player Id's + /// + /// + /// + /// + public static bool ContainsPlayer(this IEnumerable players, IPlayer playerToCheck) + { + if (playerToCheck == null) + { + return false; + } + + foreach (IPlayer player in players) + { + if (player.Id == playerToCheck.Id) + { + return true; + } + } + + return false; + } } } diff --git a/project/SPT.Custom/Patches/IsEnemyPatch.cs b/project/SPT.Custom/Patches/IsEnemyPatch.cs index c0dae1e..50197cb 100644 --- a/project/SPT.Custom/Patches/IsEnemyPatch.cs +++ b/project/SPT.Custom/Patches/IsEnemyPatch.cs @@ -32,33 +32,33 @@ namespace SPT.Custom.Patches // Check existing enemies list // Could also check x.Value.Player?.Id - BSG do it this way - if (!__instance.Enemies.IsNullOrEmpty() && __instance.Enemies.Any(x => x.Key?.Id == requester.Id)) + if (__instance.Enemies != null && __instance.Enemies.Keys.ContainsPlayer(requester)) { __result = true; return false; // Skip original } // Do not force bots to be enemies if they are allies - if (!__instance.Allies.IsNullOrEmpty() && __instance.Allies.Any(x => x?.Id == requester.Id)) + if (__instance.Allies != null && __instance.Allies.ContainsPlayer(requester)) { __result = false; return false; // Skip original } // Bots should not become hostile with their group members here. This is needed in case mods add mixed groups (i.e. BEAR's and USEC's). - if (__instance.GetAllMembers().Any(i => i?.Id == requester.Id)) + if (__instance.GetAllMembers().ContainsPlayer(requester)) { __result = false; return false; // Skip original } if (__instance.InitialBotType == WildSpawnType.peacefullZryachiyEvent - || __instance.InitialBotType == WildSpawnType.shooterBTR - || __instance.InitialBotType == WildSpawnType.gifter - || __instance.InitialBotType == WildSpawnType.sectantWarrior - || __instance.InitialBotType == WildSpawnType.sectantPriest - || __instance.InitialBotType == WildSpawnType.sectactPriestEvent - || __instance.InitialBotType == WildSpawnType.ravangeZryachiyEvent + || __instance.InitialBotType == WildSpawnType.shooterBTR + || __instance.InitialBotType == WildSpawnType.gifter + || __instance.InitialBotType == WildSpawnType.sectantWarrior + || __instance.InitialBotType == WildSpawnType.sectantPriest + || __instance.InitialBotType == WildSpawnType.sectactPriestEvent + || __instance.InitialBotType == WildSpawnType.ravangeZryachiyEvent || __instance.InitialBotType == WildSpawnType.bossZryachiy || __instance.InitialBotType == WildSpawnType.followerZryachiy) {