diff --git a/project/Aki.Custom/CustomAI/AIBrainSpawnWeightAdjustment.cs b/project/Aki.Custom/CustomAI/AIBrainSpawnWeightAdjustment.cs index 42d4fda..10e5a17 100644 --- a/project/Aki.Custom/CustomAI/AIBrainSpawnWeightAdjustment.cs +++ b/project/Aki.Custom/CustomAI/AIBrainSpawnWeightAdjustment.cs @@ -13,7 +13,6 @@ namespace Aki.Custom.CustomAI private static AIBrains aiBrainsCache = null; private static DateTime aiBrainCacheDate = new DateTime(); private static readonly Random random = new Random(); - private static readonly List playerScavTypes = new List() { WildSpawnType.bossKilla, WildSpawnType.pmcBot, WildSpawnType.bossGluhar }; private readonly ManualLogSource logger; public AIBrainSpawnWeightAdjustment(ManualLogSource logger) @@ -21,9 +20,33 @@ namespace Aki.Custom.CustomAI this.logger = logger; } - public WildSpawnType GetRandomisedPlayerScavType() + public WildSpawnType GetRandomisedPlayerScavType(BotOwner botOwner, string currentMapName) { - return playerScavTypes.Random(); + // Get map brain weights from server and cache + if (aiBrainsCache == null || CacheIsStale()) + { + ResetCacheDate(); + HydrateCacheWithServerData(); + + if (!aiBrainsCache.playerScav.TryGetValue(currentMapName.ToLower(), out _)) + { + throw new Exception($"Bots were refreshed from the server but the assault cache still doesnt contain data"); + } + } + + // Choose random weighted brain + var randomType = WeightedRandom(aiBrainsCache.playerScav[currentMapName.ToLower()].Keys.ToArray(), aiBrainsCache.playerScav[currentMapName.ToLower()].Values.ToArray()); + if (Enum.TryParse(randomType, out WildSpawnType newAiType)) + { + logger.LogWarning($"Updated player scav bot to use: {newAiType} brain"); + return newAiType; + } + else + { + logger.LogWarning($"Updated player scav bot {botOwner.Profile.Info.Nickname}: {botOwner.Profile.Info.Settings.Role} to use: {newAiType} brain"); + + return newAiType; + } } public WildSpawnType GetAssaultScavWildSpawnType(BotOwner botOwner, string currentMapName) @@ -97,6 +120,7 @@ namespace Aki.Custom.CustomAI aiBrainCacheDate = DateTime.Now; aiBrainsCache?.pmc?.Clear(); aiBrainsCache?.assault?.Clear(); + aiBrainsCache?.playerScav?.Clear(); } private static bool CacheIsStale() @@ -110,6 +134,7 @@ namespace Aki.Custom.CustomAI { public Dictionary>> pmc { get; set; } public Dictionary> assault { get; set; } + public Dictionary> playerScav { get; set; } } /// diff --git a/project/Aki.Custom/Patches/CustomAiPatch.cs b/project/Aki.Custom/Patches/CustomAiPatch.cs index b28639e..8683ca3 100644 --- a/project/Aki.Custom/Patches/CustomAiPatch.cs +++ b/project/Aki.Custom/Patches/CustomAiPatch.cs @@ -32,13 +32,14 @@ namespace Aki.Custom.Patches __state = ___botOwner_0.Profile.Info.Settings.Role; // Store original type in state param to allow access in PatchPostFix() try { + string currentMapName = GetCurrentMap(); if (AiHelpers.BotIsPlayerScav(__state, ___botOwner_0)) { - ___botOwner_0.Profile.Info.Settings.Role = aIBrainSpawnWeightAdjustment.GetRandomisedPlayerScavType(); + ___botOwner_0.Profile.Info.Settings.Role = aIBrainSpawnWeightAdjustment.GetRandomisedPlayerScavType(___botOwner_0, currentMapName); return true; // Do original } - string currentMapName = GetCurrentMap(); + if (AiHelpers.BotIsNormalAssaultScav(__state, ___botOwner_0)) { ___botOwner_0.Profile.Info.Settings.Role = aIBrainSpawnWeightAdjustment.GetAssaultScavWildSpawnType(___botOwner_0, currentMapName);