2023-10-10 10:58:33 +00:00
using Aki.Reflection.Patching ;
2023-03-03 18:52:31 +00:00
using EFT ;
using System ;
2023-07-12 17:52:32 +01:00
using Comfort.Common ;
2023-03-03 18:52:31 +00:00
using System.Reflection ;
using Aki.PrePatch ;
2023-10-10 10:58:33 +00:00
using Aki.Custom.CustomAI ;
2024-01-13 22:08:29 +00:00
using HarmonyLib ;
2023-03-03 18:52:31 +00:00
namespace Aki.Custom.Patches
{
public class CustomAiPatch : ModulePatch
{
2023-10-10 10:58:33 +00:00
private static readonly PmcFoundInRaidEquipment pmcFoundInRaidEquipment = new PmcFoundInRaidEquipment ( Logger ) ;
private static readonly AIBrainSpawnWeightAdjustment aIBrainSpawnWeightAdjustment = new AIBrainSpawnWeightAdjustment ( Logger ) ;
2023-03-03 18:52:31 +00:00
protected override MethodBase GetTargetMethod ( )
{
2024-01-13 22:08:29 +00:00
return AccessTools . Method ( typeof ( StandartBotBrain ) , nameof ( StandartBotBrain . Activate ) ) ;
2023-03-03 18:52:31 +00:00
}
/// <summary>
/// Get a randomly picked wildspawntype from server and change PMC bot to use it, this ensures the bot is generated with that random type altering its behaviour
2023-10-10 10:58:33 +00:00
/// Postfix will adjust it back to original type
2023-03-03 18:52:31 +00:00
/// </summary>
/// <param name="__state">state to save for postfix to use later</param>
/// <param name="__instance"></param>
/// <param name="___botOwner_0">botOwner_0 property</param>
[PatchPrefix]
2023-10-10 10:58:33 +00:00
private static bool PatchPrefix ( out WildSpawnType __state , StandartBotBrain __instance , BotOwner ___botOwner_0 )
2023-03-03 18:52:31 +00:00
{
2024-02-11 10:33:59 +00:00
var player = Singleton < GameWorld > . Instance . MainPlayer ;
2023-10-10 10:58:33 +00:00
___botOwner_0 . Profile . Info . Settings . Role = FixAssaultGroupPmcsRole ( ___botOwner_0 ) ;
__state = ___botOwner_0 . Profile . Info . Settings . Role ; // Store original type in state param to allow access in PatchPostFix()
2023-03-03 18:52:31 +00:00
try
{
2023-11-26 21:50:49 +00:00
string currentMapName = GetCurrentMap ( ) ;
2024-02-11 10:33:59 +00:00
if ( AiHelpers . BotIsPlayerScav ( __state , ___botOwner_0 . Profile . Info . Nickname ) )
2023-03-03 18:52:31 +00:00
{
2023-11-26 21:50:49 +00:00
___botOwner_0 . Profile . Info . Settings . Role = aIBrainSpawnWeightAdjustment . GetRandomisedPlayerScavType ( ___botOwner_0 , currentMapName ) ;
2023-03-03 18:52:31 +00:00
2023-10-10 10:58:33 +00:00
return true ; // Do original
}
2023-11-26 21:50:49 +00:00
2023-10-10 10:58:33 +00:00
if ( AiHelpers . BotIsNormalAssaultScav ( __state , ___botOwner_0 ) )
{
___botOwner_0 . Profile . Info . Settings . Role = aIBrainSpawnWeightAdjustment . GetAssaultScavWildSpawnType ( ___botOwner_0 , currentMapName ) ;
2023-03-03 18:52:31 +00:00
2023-10-10 10:58:33 +00:00
return true ; // Do original
}
2023-03-03 18:52:31 +00:00
2023-10-10 10:58:33 +00:00
if ( AiHelpers . BotIsSptPmc ( __state , ___botOwner_0 ) )
{
// Bot has inventory equipment
if ( ___botOwner_0 . Profile ? . Inventory ? . Equipment ! = null )
2023-03-03 18:52:31 +00:00
{
2023-10-10 10:58:33 +00:00
pmcFoundInRaidEquipment . ConfigurePMCFindInRaidStatus ( ___botOwner_0 ) ;
2023-03-03 18:52:31 +00:00
}
2023-10-10 10:58:33 +00:00
___botOwner_0 . Profile . Info . Settings . Role = aIBrainSpawnWeightAdjustment . GetPmcWildSpawnType ( ___botOwner_0 , ___botOwner_0 . Profile . Info . Settings . Role , currentMapName ) ;
2023-03-03 18:52:31 +00:00
}
}
catch ( Exception ex )
{
2023-10-10 10:58:33 +00:00
Logger . LogError ( $"Error running CustomAiPatch PatchPrefix(): {ex.Message}" ) ;
2023-07-12 18:09:14 +01:00
Logger . LogError ( ex . StackTrace ) ;
2023-03-03 18:52:31 +00:00
}
return true ; // Do original
}
2023-10-10 10:58:33 +00:00
/// <summary>
/// the client sometimes replaces PMC roles with 'assaultGroup', give PMCs their original role back (sptBear/sptUsec)
/// </summary>
/// <returns>WildSpawnType</returns>
private static WildSpawnType FixAssaultGroupPmcsRole ( BotOwner botOwner )
{
if ( botOwner . Profile . Info . IsStreamerModeAvailable & & botOwner . Profile . Info . Settings . Role = = WildSpawnType . assaultGroup )
{
Logger . LogError ( $"Broken PMC found: {botOwner.Profile.Nickname}, was {botOwner.Profile.Info.Settings.Role}" ) ;
// Its a PMC, figure out what the bot originally was and return it
return botOwner . Profile . Info . Side = = EPlayerSide . Bear
? ( WildSpawnType ) AkiBotsPrePatcher . sptBearValue
: ( WildSpawnType ) AkiBotsPrePatcher . sptUsecValue ;
}
// Not broken pmc, return original role
return botOwner . Profile . Info . Settings . Role ;
}
2023-03-03 18:52:31 +00:00
/// <summary>
/// Revert prefix change, get bots type back to what it was before changes
/// </summary>
/// <param name="__state">Saved state from prefix patch</param>
/// <param name="___botOwner_0">botOwner_0 property</param>
[PatchPostfix]
private static void PatchPostFix ( WildSpawnType __state , BotOwner ___botOwner_0 )
{
2023-10-10 10:58:33 +00:00
if ( AiHelpers . BotIsSptPmc ( __state , ___botOwner_0 ) )
2023-03-03 18:52:31 +00:00
{
// Set spt bot bot back to original type
___botOwner_0 . Profile . Info . Settings . Role = __state ;
}
2024-02-11 10:33:59 +00:00
else if ( AiHelpers . BotIsPlayerScav ( __state , ___botOwner_0 . Profile . Info . Nickname ) )
2023-10-10 10:58:33 +00:00
{
// Set pscav back to original type
___botOwner_0 . Profile . Info . Settings . Role = __state ;
}
2023-03-03 18:52:31 +00:00
}
private static string GetCurrentMap ( )
{
var gameWorld = Singleton < GameWorld > . Instance ;
2023-07-07 10:37:47 +01:00
return gameWorld . MainPlayer . Location ;
2023-03-03 18:52:31 +00:00
}
}
}