Decompiled source of Natural selection v0.0.2

DLLs/fandovec03.NaturalSelection.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using NaturalSelection.EnemyPatches;
using NaturalSelection.Generics;
using NaturalSelectionLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("fandovec03.NaturalSelection")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1+766b8b482af5abf0a600d1fd4e0f4c547640b682")]
[assembly: AssemblyProduct("NaturalSelection")]
[assembly: AssemblyTitle("fandovec03.NaturalSelection")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace NaturalSelection
{
	[BepInPlugin("fandovec03.NaturalSelection", "NaturalSelection", "0.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Script : BaseUnityPlugin
	{
		internal static bool stableToggle;

		internal static float clampedAgentRadius;

		public static Script Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		internal static MyModConfig BoundingConfig { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			BoundingConfig = new MyModConfig(((BaseUnityPlugin)this).Config);
			stableToggle = BoundingConfig.stableMode.Value;
			clampedAgentRadius = Mathf.Clamp(BoundingConfig.agentRadiusModifier.Value, 0.1f, 1f);
			Patch();
			Logger.LogInfo((object)"fandovec03.NaturalSelection v0.0.1 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("fandovec03.NaturalSelection");
			}
			Logger.LogInfo((object)"Patching fandovec03.NaturalSelection ...");
			Harmony.PatchAll(typeof(AICollisionDetectPatch));
			Harmony.PatchAll(typeof(EnemyAIPatch));
			if (BoundingConfig.loadSandworms.Value)
			{
				Harmony.PatchAll(typeof(SandWormAIPatch));
			}
			if (BoundingConfig.loadBlob.Value)
			{
				Harmony.PatchAll(typeof(BlobAIPatch));
			}
			if (BoundingConfig.loadHoardingBugs.Value)
			{
				Harmony.PatchAll(typeof(HoarderBugPatch));
			}
			if (BoundingConfig.LoadBees.Value)
			{
				Harmony.PatchAll(typeof(BeeAIPatch));
			}
			if (BoundingConfig.loadGiants.Value)
			{
				Harmony.PatchAll(typeof(ForestGiantPatch));
			}
			if (!stableToggle)
			{
				if (BoundingConfig.loadNutcrackers.Value)
				{
					Harmony.PatchAll(typeof(NutcrackerAIPatch));
				}
				if (BoundingConfig.loadSporeLizard.Value)
				{
					Harmony.PatchAll(typeof(PufferAIPatch));
				}
				if (BoundingConfig.loadSpiders.Value)
				{
					Harmony.PatchAll(typeof(SandSpiderAIPatch));
				}
				Logger.LogInfo((object)"Stable mode off. Loaded all patches.");
			}
			else
			{
				Logger.LogInfo((object)"Stable mode on. Excluded unstable and WIP patches from loading.");
			}
			Logger.LogInfo((object)"Finished patching fandovec03.NaturalSelection !");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "fandovec03.NaturalSelection";

		public const string PLUGIN_NAME = "NaturalSelection";

		public const string PLUGIN_VERSION = "0.0.1";
	}
}
namespace NaturalSelection.Generics
{
	internal class MyModConfig
	{
		public readonly ConfigEntry<bool> stableMode;

		public readonly ConfigEntry<bool> spiderHuntHoardingbug;

		public readonly ConfigEntry<float> agentRadiusModifier;

		public readonly ConfigEntry<bool> enableSpider;

		public readonly ConfigEntry<bool> enableSlime;

		public readonly ConfigEntry<bool> enableLeviathan;

		public readonly ConfigEntry<bool> enableSporeLizard;

		public readonly ConfigEntry<bool> enableRedBees;

		public readonly ConfigEntry<bool> enableNutcrackers;

		public readonly ConfigEntry<bool> loadNutcrackers;

		public readonly ConfigEntry<bool> loadSpiders;

		public readonly ConfigEntry<bool> loadSandworms;

		public readonly ConfigEntry<bool> loadGiants;

		public readonly ConfigEntry<bool> loadHoardingBugs;

		public readonly ConfigEntry<bool> loadBlob;

		public readonly ConfigEntry<bool> LoadBees;

		public readonly ConfigEntry<bool> loadSporeLizard;

		public readonly ConfigEntry<bool> debugBool;

		public readonly ConfigEntry<bool> spammyLogs;

		public readonly ConfigEntry<bool> debugTriggerFlags;

		public readonly ConfigEntry<bool> debugRedBees;

		public readonly ConfigEntry<bool> debugSandworms;

		public readonly ConfigEntry<bool> debugHygrodere;

		public readonly ConfigEntry<bool> debugNutcrackers;

		public readonly ConfigEntry<bool> debugSpiders;

		public readonly ConfigEntry<bool> debugGiants;

		public readonly ConfigEntry<bool> debugUnspecified;

		public MyModConfig(ConfigFile cfg)
		{
			cfg.SaveOnConfigSet = false;
			stableMode = cfg.Bind<bool>("General Settings", "Toggle stable mode", true, "When true, the mod will exlude patches that are WIP or are experimental from loading");
			agentRadiusModifier = cfg.Bind<float>("WIP", "Agent radius modifier", 0.6f, "Agent radius multiplier. Agent size is modified to make collisions more reliable. Lower multiplier makes final Agent radius smaller. \n \n [Values not between 0.1 and 1 are Clamped]");
			agentRadiusModifier.Value = Mathf.Clamp(agentRadiusModifier.Value, 0.1f, 1f);
			spiderHuntHoardingbug = cfg.Bind<bool>("WIP", "Spider hunts Hoarding bugs", false, "Bunker spider chases and hunts hoarding bugs. DEV ONLY");
			enableSpider = cfg.Bind<bool>("WIP", "Enable spider", false, "Mod applies changes Bunker Spider. DEV ONLY");
			enableSlime = cfg.Bind<bool>("Entity settings", "Enable slime", true, "Mod applies changes Hygrodere. Slime now damages every entity it passes by.");
			enableLeviathan = cfg.Bind<bool>("Entity settings", "Enable leviathan", true, "Mod applies changes Earth leviathan. Leviathan now targets other creatures aswell.");
			enableSporeLizard = cfg.Bind<bool>("WIP", "Enable SporeLizard", false, "Mod applies changes Spore lizard. It is now mortal!");
			enableRedBees = cfg.Bind<bool>("Entity settings", "Enable Red bees (Circuit bees)", true, "Mod applies changes red bees. They now defend nest from other mobs and kill everything in rampage!");
			enableNutcrackers = cfg.Bind<bool>("WIP", "Enable Nutcrackers", false, "Mod applies changes to nutcrackers. DEV ONLY");
			loadSpiders = cfg.Bind<bool>("Initialization settings (Not recommended)", "Load spider patches", true, "Load the spider patches. Do not touch.");
			loadBlob = cfg.Bind<bool>("Initialization settings (Not recommended)", "Load slime patches", true, "Load the slime patches. Do not touch.");
			loadSandworms = cfg.Bind<bool>("Initialization settings (Not recommended)", "Load leviathan patches", true, "Load the leviathan patches. Do not touch.");
			loadGiants = cfg.Bind<bool>("Initialization settings (Not recommended)", "Load giant patches", true, "Load the giant patches. Do not touch.");
			LoadBees = cfg.Bind<bool>("Initialization settings (Not recommended)", "Load circuit bees patches", true, "Load bees patches. Do not touch.");
			loadNutcrackers = cfg.Bind<bool>("Initialization settings (Not recommended)", "Load nutcracker patches", true, "Load the nutcracker patches. Do not touch.");
			loadHoardingBugs = cfg.Bind<bool>("Initialization settings (Not recommended)", "Load hoarding bugs patches", true, "Load the hoarding bug patches. Do not touch.");
			loadSporeLizard = cfg.Bind<bool>("Initialization settings (Not recommended)", "Load spore lizards patches", true, "Load the spore lizard patches. Do not touch.");
			debugBool = cfg.Bind<bool>("Debug", "Debug mode", false, "Enables debug mode for more debug logs.");
			spammyLogs = cfg.Bind<bool>("Debug", "Spammy logs", false, "Enables spammy logs for extra logs.");
			debugTriggerFlags = cfg.Bind<bool>("Debug", "Trigger flags", false, "Enables logs with trigger flag.");
			debugRedBees = cfg.Bind<bool>("Debug", "Log bees", false, "Enables logs for bees.");
			debugSandworms = cfg.Bind<bool>("Debug", "Log sandworms", false, "Enables logs for sandowrms.");
			debugHygrodere = cfg.Bind<bool>("Debug", "Log hydrogere", false, "Enables logs for hydrogere.");
			debugNutcrackers = cfg.Bind<bool>("Debug", "Log nutcrackers", false, "Enables logs for nutcrackers.");
			debugSpiders = cfg.Bind<bool>("Debug", "Log spiders", false, "Enables logs for spiders.");
			debugGiants = cfg.Bind<bool>("Debug", "Log giants", false, "Enables logs for giants.");
			debugUnspecified = cfg.Bind<bool>("Debug", "Log unspecified", false, "Enables logs for unspecified.");
			ClearOrphanedEntries(cfg);
			cfg.Save();
			cfg.SaveOnConfigSet = true;
		}

		public void ClearOrphanedEntries(ConfigFile cfg)
		{
			PropertyInfo propertyInfo = AccessTools.Property(typeof(ConfigFile), "OrphanedEntries");
			Dictionary<ConfigDefinition, string> dictionary = (Dictionary<ConfigDefinition, string>)propertyInfo.GetValue(cfg);
			dictionary.Clear();
		}
	}
}
namespace NaturalSelection.EnemyPatches
{
	internal class BeeValues
	{
		public EnemyAI? closestEnemy = null;

		public EnemyAI? targetEnemy = null;

		public Vector3 lastKnownEnemyPosition = Vector3.zero;

		public int customBehaviorStateIndex = 0;

		public float timeSinceHittingEnemy = 0f;

		public float LostLOSOfEnemy = 0f;

		public List<Type> enemyTypes = new List<Type>();
	}
	[HarmonyPatch(typeof(RedLocustBees))]
	internal class BeeAIPatch
	{
		private static Dictionary<RedLocustBees, BeeValues> beeList = new Dictionary<RedLocustBees, BeeValues>();

		private static List<EnemyAI> enemyList = new List<EnemyAI>();

		private static bool logBees = Script.BoundingConfig.debugRedBees.Value;

		private static bool debugSpam = Script.BoundingConfig.spammyLogs.Value;

		private static float UpdateTimer = 0f;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPatch(RedLocustBees __instance)
		{
			if (!beeList.ContainsKey(__instance))
			{
				beeList.Add(__instance, new BeeValues());
			}
			BeeValues beeValues = beeList[__instance];
			if (beeValues.enemyTypes.Count < 1)
			{
				beeValues.enemyTypes.Add(typeof(DocileLocustBeesAI));
				beeValues.enemyTypes.Add(typeof(SandWormAI));
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePatch(RedLocustBees __instance)
		{
			if (UpdateTimer <= 0f)
			{
				enemyList = EnemyAIPatch.filterEnemyList(EnemyAIPatch.GetOutsideEnemyList(EnemyAIPatch.GetCompleteList((EnemyAI)(object)__instance), (EnemyAI)(object)__instance), beeList[__instance].enemyTypes, (EnemyAI)(object)__instance, inverseToggle: true);
				if (enemyList.Contains((EnemyAI)(object)__instance))
				{
					if (logBees && debugSpam)
					{
						Script.Logger.LogError((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " FOUND ITSELF IN THE EnemyList! Removing..."));
					}
					enemyList.Remove((EnemyAI)(object)__instance);
				}
				UpdateTimer = 0.2f;
			}
			else
			{
				UpdateTimer -= Time.deltaTime;
			}
		}

		[HarmonyPatch("DoAIInterval")]
		[HarmonyPrefix]
		private static bool DoAIIntervalPrefixPatch(RedLocustBees __instance)
		{
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			BeeValues beeValues = beeList[__instance];
			if ((Object)(object)beeValues.targetEnemy != (Object)null && !((EnemyAI)__instance).movingTowardsTargetPlayer && ((EnemyAI)__instance).currentBehaviourStateIndex != 0)
			{
				if (logBees && debugSpam)
				{
					Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: Prefix triggered false"));
				}
				if (((EnemyAI)__instance).moveTowardsDestination)
				{
					((EnemyAI)__instance).agent.SetDestination(((EnemyAI)__instance).destination);
				}
				((EnemyAI)__instance).SyncPositionToClients();
				return false;
			}
			if (logBees && debugSpam)
			{
				Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: Prefix triggered true"));
			}
			return true;
		}

		[HarmonyPatch("DoAIInterval")]
		[HarmonyPostfix]
		private static void DoAIIntervalPostfixPatch(RedLocustBees __instance)
		{
			//IL_0230: Unknown result type (might be due to invalid IL or missing references)
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_0250: Unknown result type (might be due to invalid IL or missing references)
			//IL_02be: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0541: Unknown result type (might be due to invalid IL or missing references)
			//IL_0546: Unknown result type (might be due to invalid IL or missing references)
			//IL_0550: Unknown result type (might be due to invalid IL or missing references)
			//IL_0555: Unknown result type (might be due to invalid IL or missing references)
			//IL_055a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0593: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_034c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0830: Unknown result type (might be due to invalid IL or missing references)
			//IL_085e: Unknown result type (might be due to invalid IL or missing references)
			//IL_065a: Unknown result type (might be due to invalid IL or missing references)
			//IL_066a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0692: Unknown result type (might be due to invalid IL or missing references)
			//IL_0ad7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a95: Unknown result type (might be due to invalid IL or missing references)
			//IL_09d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_09da: Unknown result type (might be due to invalid IL or missing references)
			//IL_09e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_06ea: Unknown result type (might be due to invalid IL or missing references)
			BeeValues beeValues = beeList[__instance];
			switch (((EnemyAI)__instance).currentBehaviourStateIndex)
			{
			case 0:
			{
				EnemyAI val3 = null;
				if (EnemyAIPatch.GetEnemiesInLOS((EnemyAI)(object)__instance, enemyList, 360f, 16, 1f).Count > 0)
				{
					if (enemyList.Contains((EnemyAI)(object)__instance))
					{
						if (logBees && debugSpam)
						{
							Script.Logger.LogError((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " FOUND ITSELF IN THE EnemyList before LOSEnemy! Removing..."));
						}
						enemyList.Remove((EnemyAI)(object)__instance);
					}
					val3 = EnemyAIPatch.GetEnemiesInLOS((EnemyAI)(object)__instance, enemyList, 360f, 16, 1f).Keys.First();
					if (logBees)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case0: Checked LOS for enemies. Enemy found: " + EnemyAIPatch.DebugStringHead(val3)));
					}
					if (logBees && debugSpam)
					{
						foreach (KeyValuePair<EnemyAI, float> enemiesInLO in EnemyAIPatch.GetEnemiesInLOS((EnemyAI)(object)__instance, enemyList, 360f, 16, 1f))
						{
							Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Checking the LOSList: " + EnemyAIPatch.DebugStringHead(enemiesInLO.Key) + ", Distance: " + enemiesInLO.Value));
							if ((Object)(object)enemiesInLO.Key == (Object)(object)__instance)
							{
								Script.Logger.LogError((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " FOUND ITSELF IN THE LOSList: " + EnemyAIPatch.DebugStringHead(enemiesInLO.Key) + ", Distance: " + enemiesInLO.Value));
							}
						}
					}
				}
				if (__instance.wasInChase)
				{
					__instance.wasInChase = false;
				}
				if (Vector3.Distance(((Component)__instance).transform.position, __instance.lastKnownHivePosition) > 2f)
				{
					((EnemyAI)__instance).SetDestinationToPosition(__instance.lastKnownHivePosition, true);
				}
				if (__instance.IsHiveMissing())
				{
					((EnemyAI)__instance).SwitchToBehaviourState(2);
					beeValues.customBehaviorStateIndex = 2;
					if (logBees)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case0: HIVE IS MISSING! CustomBehaviorStateIndex changed: " + beeValues.customBehaviorStateIndex));
					}
				}
				else if ((Object)(object)val3 != (Object)null && Vector3.Distance(((Component)val3).transform.position, ((Component)__instance.hive).transform.position) < (float)__instance.defenseDistance)
				{
					((EnemyAI)__instance).SetDestinationToPosition(((Component)val3).transform.position, true);
					if (logBees)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case0: Moving towards " + (object)val3));
					}
					beeValues.customBehaviorStateIndex = 1;
					((EnemyAI)__instance).SwitchToBehaviourState(1);
					__instance.syncedLastKnownHivePosition = false;
					__instance.SyncLastKnownHivePositionServerRpc(__instance.lastKnownHivePosition);
					beeValues.LostLOSOfEnemy = 0f;
					if (logBees)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case0: CustomBehaviorStateIndex changed: " + beeValues.customBehaviorStateIndex));
					}
				}
				break;
			}
			case 1:
				if (((Object)(object)((EnemyAI)__instance).targetPlayer != (Object)null && ((EnemyAI)__instance).movingTowardsTargetPlayer) || (!((Object)(object)beeValues.targetEnemy == (Object)null) && !beeValues.targetEnemy.isEnemyDead && !(Vector3.Distance(((Component)beeValues.targetEnemy).transform.position, ((Component)__instance.hive).transform.position) > (float)__instance.defenseDistance + 5f)))
				{
					break;
				}
				beeValues.targetEnemy = null;
				__instance.wasInChase = false;
				if (__instance.IsHiveMissing())
				{
					beeValues.customBehaviorStateIndex = 2;
					((EnemyAI)__instance).SwitchToBehaviourState(2);
					if (logBees)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case1: HIVE IS MISSING! CustomBehaviorStateIndex changed: " + beeValues.customBehaviorStateIndex));
					}
				}
				else
				{
					beeValues.customBehaviorStateIndex = 0;
					((EnemyAI)__instance).SwitchToBehaviourState(0);
					if (logBees)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case1: CustomBehaviorStateIndex changed: " + beeValues.customBehaviorStateIndex));
					}
				}
				break;
			case 2:
			{
				if ((Object)(object)((EnemyAI)__instance).targetPlayer != (Object)null || ((EnemyAI)__instance).movingTowardsTargetPlayer)
				{
					if (logBees && debugSpam)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: target player found or moving towards target player"));
					}
					break;
				}
				if (__instance.IsHivePlacedAndInLOS())
				{
					if (__instance.wasInChase)
					{
						__instance.wasInChase = false;
					}
					__instance.lastKnownHivePosition = ((Component)__instance.hive).transform.position + Vector3.up * 0.5f;
					if (logBees)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: IsHivePlacedAndInLOS triggered"));
					}
					EnemyAI val = null;
					Collider[] array = Physics.OverlapSphere(((Component)__instance.hive).transform.position, (float)__instance.defenseDistance, StartOfRound.Instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)2);
					if (array != null && array.Length != 0)
					{
						for (int i = 0; i < array.Length; i++)
						{
							if (((Component)array[i]).gameObject.tag == "Enemy")
							{
								val = ((Component)array[i]).gameObject.GetComponent<EnemyAICollisionDetect>().mainScript;
								if (logBees)
								{
									Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: CollisionArray triggered. Enemy found: " + EnemyAIPatch.DebugStringHead(val)));
								}
								break;
							}
						}
					}
					if ((Object)(object)val != (Object)null && Vector3.Distance(((Component)val).transform.position, ((Component)__instance.hive).transform.position) < (float)__instance.defenseDistance)
					{
						((EnemyAI)__instance).SetDestinationToPosition(((Component)val).transform.position, true);
						if (logBees)
						{
							Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: Moving towards: " + (object)val));
						}
						beeValues.customBehaviorStateIndex = 1;
						((EnemyAI)__instance).SwitchToBehaviourState(1);
						__instance.syncedLastKnownHivePosition = false;
						__instance.SyncLastKnownHivePositionServerRpc(__instance.lastKnownHivePosition);
						beeValues.LostLOSOfEnemy = 0f;
						if (logBees)
						{
							Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: CustomBehaviorStateIndex changed: " + beeValues.customBehaviorStateIndex));
						}
					}
					else
					{
						beeValues.customBehaviorStateIndex = 0;
						((EnemyAI)__instance).SwitchToBehaviourState(0);
						if (logBees)
						{
							Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: CustomBehaviorStateIndex changed: " + beeValues.customBehaviorStateIndex));
						}
					}
					break;
				}
				bool flag = false;
				Dictionary<EnemyAI, float> enemiesInLOS = EnemyAIPatch.GetEnemiesInLOS((EnemyAI)(object)__instance, enemyList, 360f, 16, 1f);
				EnemyAI val2 = null;
				if (enemiesInLOS.Count > 0)
				{
					val2 = enemiesInLOS.Keys.First();
				}
				if (logBees)
				{
					Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: " + ((object)val2)?.ToString() + " is closest to hive."));
				}
				if ((Object)(object)val2 != (Object)null && (Object)(object)beeValues.targetEnemy != (Object)(object)val2)
				{
					flag = true;
					__instance.wasInChase = false;
					beeValues.targetEnemy = val2;
					((EnemyAI)__instance).SetDestinationToPosition(((Component)beeValues.targetEnemy).transform.position, true);
					((EnemyAI)__instance).StopSearch(__instance.searchForHive, true);
					__instance.syncedLastKnownHivePosition = false;
					beeValues.LostLOSOfEnemy = 0f;
					__instance.SyncLastKnownHivePositionServerRpc(__instance.lastKnownHivePosition);
					if (logBees)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: Targeting " + ((object)val2)?.ToString() + ". Synced hive position"));
					}
					break;
				}
				if ((Object)(object)beeValues.targetEnemy != (Object)null)
				{
					((EnemyAI)__instance).agent.acceleration = 16f;
					if (!flag && EnemyAIPatch.GetEnemiesInLOS((EnemyAI)(object)__instance, enemyList, 360f, 16, 2f).Count == 0)
					{
						if (logBees && debugSpam)
						{
							Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: lost LOS of " + ((object)beeValues.targetEnemy)?.ToString() + ", started timer."));
						}
						beeValues.LostLOSOfEnemy += ((EnemyAI)__instance).AIIntervalTime;
						if (beeValues.LostLOSOfEnemy >= 4.5f)
						{
							beeValues.targetEnemy = null;
							beeValues.LostLOSOfEnemy = 0f;
							if (logBees)
							{
								Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: lost LOS of " + ((object)beeValues.targetEnemy)?.ToString() + ", Stopped and reset timer."));
							}
						}
					}
					else
					{
						__instance.wasInChase = true;
						beeValues.lastKnownEnemyPosition = ((Component)beeValues.targetEnemy).transform.position;
						((EnemyAI)__instance).SetDestinationToPosition(beeValues.lastKnownEnemyPosition, true);
						beeValues.LostLOSOfEnemy = 0f;
						if (logBees)
						{
							Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: lost " + (object)beeValues.targetEnemy));
						}
					}
					break;
				}
				((EnemyAI)__instance).agent.acceleration = 13f;
				if (__instance.searchForHive.inProgress)
				{
					break;
				}
				if (logBees)
				{
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: set new search for hive"));
				}
				if (__instance.wasInChase)
				{
					((EnemyAI)__instance).StartSearch(beeValues.lastKnownEnemyPosition, __instance.searchForHive);
					if (logBees)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: Started search for hive."));
					}
				}
				else
				{
					((EnemyAI)__instance).StartSearch(((Component)__instance).transform.position, __instance.searchForHive);
					if (logBees)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "case2: Started search for hive."));
					}
				}
				break;
			}
			}
		}

		public static void OnCustomEnemyCollision(RedLocustBees __instance, EnemyAI mainscript2)
		{
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Expected O, but got Unknown
			if (!beeList.ContainsKey(__instance))
			{
				return;
			}
			if ((beeList[__instance].timeSinceHittingEnemy > 1.7f && ((EnemyAI)__instance).currentBehaviourStateIndex > 0) || (beeList[__instance].timeSinceHittingEnemy > 1.3f && ((EnemyAI)__instance).currentBehaviourStateIndex == 2))
			{
				mainscript2.HitEnemy(1, (PlayerControllerB)null, true, -1);
				beeList[__instance].timeSinceHittingEnemy = 0f;
				if (!(mainscript2 is ForestGiantAI) || mainscript2.currentBehaviourStateIndex == 2)
				{
					return;
				}
				float num = Random.Range(0f, 100f);
				float num2 = 0f;
				num2 = ((((EnemyAI)__instance).currentBehaviourStateIndex == 2) ? 8f : 1.5f);
				if (logBees)
				{
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "OnCustomEnemyCollision: Giant hit. Chance to set on fire: " + num2));
				}
				if (num <= num2)
				{
					if (logBees)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "OnCustomEnemyCollision: SET GIANT ON FIRE! Random number: " + num));
					}
					ForestGiantAI val = (ForestGiantAI)mainscript2;
					val.timeAtStartOfBurning = Time.realtimeSinceStartup;
					((EnemyAI)val).SwitchToBehaviourState(2);
				}
			}
			else
			{
				beeList[__instance].timeSinceHittingEnemy += Time.deltaTime;
			}
		}
	}
	internal class BlobData
	{
		public float timeSinceHittingLocalMonster = 0f;

		public EnemyAI? closestEnemy = null;
	}
	[HarmonyPatch(typeof(BlobAI))]
	public class BlobAIPatch
	{
		private static float CDtimer = 0.5f;

		private static List<EnemyAI> whiteList = new List<EnemyAI>();

		private static Dictionary<BlobAI, BlobData> slimeList = new Dictionary<BlobAI, BlobData>();

		private static bool logBlob = Script.BoundingConfig.debugHygrodere.Value;

		[HarmonyPatch("Start")]
		[HarmonyPrefix]
		private static void StartPatch(BlobAI __instance)
		{
			if (!slimeList.ContainsKey(__instance))
			{
				slimeList.Add(__instance, new BlobData());
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static void BlobUpdatePatch(BlobAI __instance)
		{
			BlobData blobData = slimeList[__instance];
			blobData.timeSinceHittingLocalMonster += Time.deltaTime;
			if (CDtimer > 0f)
			{
				CDtimer -= Time.deltaTime;
			}
			if (!(CDtimer <= 0f))
			{
				return;
			}
			List<EnemyAI> list = new List<EnemyAI>();
			list = EnemyAIPatch.GetInsideEnemyList(EnemyAIPatch.GetCompleteList((EnemyAI)(object)__instance), (EnemyAI)(object)__instance);
			for (int i = 0; i < list.Count; i++)
			{
				if (Object.op_Implicit((Object)(object)list[i]) == IsEnemyImmortal.EnemyIsImmortal(list[i]))
				{
					continue;
				}
				if (list[i] is NutcrackerEnemyAI)
				{
					if (logBlob)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + ((object)list[i])?.ToString() + " is blacklisted!"));
					}
					continue;
				}
				if (!whiteList.Contains(list[i]))
				{
					if (logBlob)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Added " + ((object)list[i])?.ToString() + " to whitelist"));
					}
					whiteList.Add(list[i]);
				}
				if (whiteList.Contains(list[i]) && logBlob)
				{
					Script.Logger.LogWarning((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + ((object)list[i])?.ToString() + " is already in the whitelist"));
				}
			}
			for (int j = 0; j < whiteList.Count; j++)
			{
				if ((Object)(object)whiteList[j] == (Object)null)
				{
					if (logBlob)
					{
						Script.Logger.LogError((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " found NULL enemz in whitelist. removing."));
					}
					whiteList.Remove(whiteList[j]);
				}
			}
			CDtimer = 0.5f;
		}

		public static void OnCustomEnemyCollision(BlobAI __instance, EnemyAI mainscript2)
		{
			if (!slimeList.ContainsKey(__instance))
			{
				return;
			}
			BlobData blobData = slimeList[__instance];
			if (!(blobData.timeSinceHittingLocalMonster > 1.5f) || mainscript2 is NutcrackerEnemyAI || mainscript2 is CaveDwellerAI || ((EnemyAI)__instance).isEnemyDead)
			{
				return;
			}
			blobData.timeSinceHittingLocalMonster = 0f;
			if (mainscript2 is FlowermanAI)
			{
				FlowermanAI val = (FlowermanAI)(object)((mainscript2 is FlowermanAI) ? mainscript2 : null);
				if ((Object)(object)val != (Object)null)
				{
					float angerMeter = val.angerMeter;
					bool isInAngerMode = val.isInAngerMode;
					((EnemyAI)val).HitEnemy(1, (PlayerControllerB)null, true, -1);
					if (mainscript2.enemyHP <= 0)
					{
						mainscript2.KillEnemyOnOwnerClient(false);
					}
					((EnemyAI)val).targetPlayer = null;
					((EnemyAI)val).movingTowardsTargetPlayer = false;
					val.isInAngerMode = false;
					val.angerMeter = angerMeter;
					val.isInAngerMode = isInAngerMode;
				}
			}
			else if (mainscript2 is HoarderBugAI)
			{
				HoarderBugAI val2 = (HoarderBugAI)(object)((mainscript2 is HoarderBugAI) ? mainscript2 : null);
				if ((Object)(object)val2 != (Object)null)
				{
					HoarderBugPatch.CustomOnHit(1, (EnemyAI)(object)__instance, playHitSFX: true, val2);
					if (mainscript2.enemyHP <= 0)
					{
						mainscript2.KillEnemyOnOwnerClient(false);
					}
				}
			}
			else
			{
				blobData.timeSinceHittingLocalMonster = 0f;
				mainscript2.HitEnemy(1, (PlayerControllerB)null, true, -1);
				if (mainscript2.enemyHP <= 0)
				{
					mainscript2.KillEnemyOnOwnerClient(false);
				}
			}
		}
	}
	public class OnCollideWithUniversal
	{
		private static bool enableSpider = Script.BoundingConfig.enableSpider.Value;

		private static bool enableSlime = Script.BoundingConfig.enableSlime.Value;

		private static bool logUnspecified = Script.BoundingConfig.debugUnspecified.Value;

		private static bool logSpider = Script.BoundingConfig.debugSpiders.Value;

		private static bool debugSpam = Script.BoundingConfig.spammyLogs.Value;

		public static void Collide(string text, EnemyAI? mainscript, EnemyAI? mainscript2)
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Expected O, but got Unknown
			//IL_020b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Expected O, but got Unknown
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Expected O, but got Unknown
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Expected O, but got Unknown
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Expected O, but got Unknown
			if (logUnspecified)
			{
				Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead(mainscript) + "Hit collider of " + EnemyAIPatch.DebugStringHead(mainscript2) + ", Tag: " + text));
			}
			if (!((Object)(object)mainscript != (Object)null) || text == "Player")
			{
			}
			if (!((Object)(object)mainscript != (Object)null) || !((Object)(object)mainscript2 != (Object)null))
			{
				return;
			}
			if (mainscript is SandSpiderAI && !(mainscript2 is SandSpiderAI) && (Object)(object)mainscript2 != (Object)null && enableSpider)
			{
				SandSpiderAI val = (SandSpiderAI)mainscript;
				if (logSpider)
				{
					Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead(mainscript) + " timeSinceHittingPlayer: " + val.timeSinceHittingPlayer));
				}
				if (val.timeSinceHittingPlayer > 1f)
				{
					val.timeSinceHittingPlayer = 0f;
					((EnemyAI)val).creatureSFX.PlayOneShot(val.attackSFX);
					if (mainscript2 is HoarderBugAI)
					{
						if (mainscript2.enemyHP > 2)
						{
							mainscript2.HitEnemy(2, (PlayerControllerB)null, true, -1);
						}
						else
						{
							mainscript2.HitEnemy(1, (PlayerControllerB)null, true, -1);
						}
					}
					if (mainscript2 is PufferAI)
					{
						if (mainscript2.enemyHP > 2)
						{
							PufferAIPatch.CustomOnHit(2, mainscript, playHitSFX: true, (PufferAI)mainscript2);
						}
						else
						{
							PufferAIPatch.CustomOnHit(1, mainscript, playHitSFX: true, (PufferAI)mainscript2);
						}
					}
					if (logSpider)
					{
						Script.Logger.LogMessage((object)(EnemyAIPatch.DebugStringHead(mainscript) + " Hit " + EnemyAIPatch.DebugStringHead(mainscript2) + ", Tag: " + text));
					}
				}
			}
			if (mainscript is BlobAI && !(mainscript2 is BlobAI) && (Object)(object)mainscript2 != (Object)null && enableSlime)
			{
				BlobAIPatch.OnCustomEnemyCollision((BlobAI)mainscript, mainscript2);
			}
			if (mainscript is RedLocustBees && !(mainscript2 is RedLocustBees) && (Object)(object)mainscript2 != (Object)null)
			{
				BeeAIPatch.OnCustomEnemyCollision((RedLocustBees)mainscript, mainscript2);
			}
		}
	}
	[HarmonyPatch(typeof(EnemyAICollisionDetect), "OnTriggerStay")]
	public class AICollisionDetectPatch
	{
		private static bool Prefix(Collider other, EnemyAICollisionDetect __instance)
		{
			EnemyAICollisionDetect component = ((Component)other).gameObject.GetComponent<EnemyAICollisionDetect>();
			if ((Object)(object)__instance != (Object)null)
			{
				if (((Component)other).CompareTag("Player") && !__instance.mainScript.isEnemyDead)
				{
					OnCollideWithUniversal.Collide("Player", null, null);
					return true;
				}
				if (((Component)other).CompareTag("Enemy") && (Object)(object)component != (Object)null && (Object)(object)component.mainScript != (Object)(object)__instance.mainScript && !component.mainScript.isEnemyDead && !IsEnemyImmortal.EnemyIsImmortal(component.mainScript))
				{
					OnCollideWithUniversal.Collide("Enemy", __instance.mainScript, component.mainScript);
					return true;
				}
			}
			return true;
		}
	}
	public class IsEnemyImmortal
	{
		public static bool EnemyIsImmortal(EnemyAI instance)
		{
			if (instance is NutcrackerEnemyAI && instance.currentBehaviourStateIndex == 0)
			{
				return true;
			}
			if (instance is JesterAI)
			{
				return true;
			}
			if (instance is BlobAI)
			{
				return true;
			}
			if (instance is SpringManAI)
			{
				return true;
			}
			if (instance is SandWormAI)
			{
				return true;
			}
			if (instance is ButlerBeesEnemyAI)
			{
				return true;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(EnemyAI))]
	internal class EnemyAIPatch
	{
		private static List<EnemyAI> enemyList = new List<EnemyAI>();

		private static float refreshCDtime = 1f;

		private static bool debugUnspecified = Script.BoundingConfig.debugUnspecified.Value;

		private static bool debugSpam = Script.BoundingConfig.spammyLogs.Value;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPostfix(EnemyAI __instance)
		{
			Script.Logger.LogInfo((object)"Called Setup library!");
			NaturalSelectionLib.LibrarySetup(Script.Logger, debugSpam, debugUnspecified);
			Collider[] componentsInChildren = ((Component)__instance).gameObject.GetComponentsInChildren<Collider>();
			foreach (Collider val in componentsInChildren)
			{
				if (!val.isTrigger)
				{
					val.isTrigger = true;
					Script.Logger.LogInfo((object)"Found non-trigger collider.");
				}
			}
			__instance.agent.radius = __instance.agent.radius * Script.clampedAgentRadius;
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePostfixPatch(EnemyAI __instance)
		{
			if (refreshCDtime <= 0f)
			{
				Script.Logger.LogInfo((object)"Called library!");
				NaturalSelectionLib.enemyListUpdate(__instance, refreshCDtime);
				refreshCDtime = 1f;
			}
			else
			{
				refreshCDtime -= Time.deltaTime;
			}
		}

		public static string DebugStringHead(EnemyAI? __instance)
		{
			Script.Logger.LogInfo((object)"Called library!");
			return NaturalSelectionLib.DebugStringHead(__instance);
		}

		public static List<EnemyAI> GetCompleteList(EnemyAI instance, bool FilterThemselves = true)
		{
			Script.Logger.LogInfo((object)"Called library!");
			return NaturalSelectionLib.GetCompleteList(instance, FilterThemselves);
		}

		public static List<EnemyAI> GetOutsideEnemyList(List<EnemyAI> importEnemyList, EnemyAI instance)
		{
			Script.Logger.LogInfo((object)"Called library!");
			return NaturalSelectionLib.GetOutsideEnemyList(importEnemyList, instance);
		}

		public static List<EnemyAI> GetInsideEnemyList(List<EnemyAI> importEnemyList, EnemyAI instance)
		{
			Script.Logger.LogInfo((object)"Called library!");
			return NaturalSelectionLib.GetInsideEnemyList(importEnemyList, instance);
		}

		public static EnemyAI? findClosestEnemy(List<EnemyAI> importEnemyList, EnemyAI? importClosestEnemy, EnemyAI __instance)
		{
			Script.Logger.LogInfo((object)"Called library!");
			return NaturalSelectionLib.findClosestEnemy(importEnemyList, importClosestEnemy, __instance);
		}

		public static List<EnemyAI> filterEnemyList(List<EnemyAI> importEnemyList, List<Type> targetTypes, EnemyAI instance, bool inverseToggle = false)
		{
			Script.Logger.LogInfo((object)"Called library!");
			return NaturalSelectionLib.filterEnemyList(importEnemyList, targetTypes, instance, inverseToggle);
		}

		public static Dictionary<EnemyAI, float> GetEnemiesInLOS(EnemyAI instance, List<EnemyAI> importEnemyList, float width = 45f, int importRange = 0, float proximityAwareness = -1f)
		{
			Script.Logger.LogInfo((object)"Called library!");
			return NaturalSelectionLib.GetEnemiesInLOS(instance, importEnemyList, width, importRange, proximityAwareness);
		}

		public static int ReactToHit(int force = 0, EnemyAI? enemyAI = null, PlayerControllerB? player = null)
		{
			if (force > 0)
			{
				return 1;
			}
			if (force > 1)
			{
				return 2;
			}
			return 0;
		}
	}
	public class ReversePatchEnemy : EnemyAI
	{
		public override void Update()
		{
			((EnemyAI)this).Update();
		}
	}
	internal class GiantData
	{
		public bool logGiant = Script.BoundingConfig.debugGiants.Value;

		private static bool debugSpam = Script.BoundingConfig.spammyLogs.Value;

		public bool? extinguish = null;
	}
	[HarmonyPatch(typeof(ForestGiantAI))]
	internal class ForestGiantPatch
	{
		private static Dictionary<ForestGiantAI, GiantData> giantDictionary = new Dictionary<ForestGiantAI, GiantData>();

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void startPostfix(ForestGiantAI __instance)
		{
			if (!giantDictionary.ContainsKey(__instance))
			{
				giantDictionary.Add(__instance, new GiantData());
			}
		}

		[HarmonyPatch("KillEnemy")]
		[HarmonyPrefix]
		public static void KillEnemyPatchPrefix(ForestGiantAI __instance, out bool __state)
		{
			GiantData giantData = giantDictionary[__instance];
			if (__instance.burningParticlesContainer.activeSelf)
			{
				__state = true;
			}
			else
			{
				__state = false;
			}
			if (giantData.logGiant)
			{
				Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " state status: " + __state));
			}
		}

		[HarmonyPatch("KillEnemy")]
		[HarmonyPostfix]
		private static void KillEnemyPatchPostfix(ForestGiantAI __instance, bool __state)
		{
			GiantData giantData = giantDictionary[__instance];
			if (__state)
			{
				__instance.burningParticlesContainer.SetActive(true);
			}
			else
			{
				__instance.burningParticlesContainer.SetActive(false);
			}
			if (giantData.logGiant)
			{
				Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " state status2: " + __state));
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static bool UpdatePrefix(ForestGiantAI __instance)
		{
			GiantData giantData = giantDictionary[__instance];
			if (((EnemyAI)__instance).currentBehaviourStateIndex == 2 && ((NetworkBehaviour)__instance).IsOwner && Time.realtimeSinceStartup - __instance.timeAtStartOfBurning > 9.5f && ((EnemyAI)__instance).enemyHP > 20 && !giantData.extinguish.HasValue)
			{
				int num = Random.Range(0, 100);
				if (num <= 33)
				{
					((EnemyAI)__instance).enemyHP = ((EnemyAI)__instance).enemyHP - 20;
					__instance.burningParticlesContainer.SetActive(false);
					__instance.giantBurningAudio.Stop();
					((EnemyAI)__instance).creatureAnimator.SetBool("burning", false);
					((EnemyAI)__instance).SwitchToBehaviourState(0);
					giantData.extinguish = true;
					if (giantData.logGiant)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " successfully extinguished itself. Skipping Update. Rolled " + num));
					}
					return false;
				}
				giantData.extinguish = false;
				if (giantData.logGiant)
				{
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " failed to extinguish itself. rolled " + num));
				}
			}
			return true;
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePostfix(ForestGiantAI __instance)
		{
			if (((EnemyAI)__instance).isEnemyDead && Time.realtimeSinceStartup - __instance.timeAtStartOfBurning < 20f && ((EnemyAI)__instance).currentBehaviourStateIndex == 2)
			{
				if (!__instance.giantBurningAudio.isPlaying)
				{
					__instance.giantBurningAudio.Play();
				}
				__instance.giantBurningAudio.volume = Mathf.Min(__instance.giantBurningAudio.volume + Time.deltaTime * 0.5f, 1f);
			}
			else if (((EnemyAI)__instance).isEnemyDead && Time.realtimeSinceStartup - __instance.timeAtStartOfBurning > 26f)
			{
				__instance.burningParticlesContainer.SetActive(false);
			}
		}
	}
	internal class HoarderBugValues
	{
		public EnemyAI? targetEnemy = null;

		public EnemyAI? closestEnemy = null;

		public bool alertedByEnemy = false;

		public List<EnemyAI> enemies = new List<EnemyAI>();

		public List<EnemyAI> enemiesInLOS = new List<EnemyAI>();
	}
	[HarmonyPatch(typeof(HoarderBugAI))]
	internal class HoarderBugPatch
	{
		private static Dictionary<HoarderBugAI, HoarderBugValues> hoarderBugList = new Dictionary<HoarderBugAI, HoarderBugValues>();

		public static void CustomOnHit(int force, EnemyAI enemyWhoHit, bool playHitSFX, HoarderBugAI __instance)
		{
			((EnemyAI)__instance).enemyHP = ((EnemyAI)__instance).enemyHP - force;
			Script.Logger.LogDebug((object)"Hoarderbug CustomHit Triggered");
			((EnemyAI)__instance).creatureVoice.PlayOneShot(__instance.hitPlayerSFX);
			RoundManager.PlayRandomClip(((EnemyAI)__instance).creatureVoice, __instance.angryScreechSFX, true, 1f, 0, 1000);
			((EnemyAI)__instance).SwitchToBehaviourState(1);
			if (((EnemyAI)__instance).enemyHP <= 0)
			{
				((EnemyAI)__instance).KillEnemy(false);
			}
		}
	}
	internal class NutcrackerData
	{
		public EnemyAI? closestEnemy = null;

		public EnemyAI? targetEnemy = null;

		public bool SeeMovingEnemy = false;

		public Vector3 lastSeenEnemyPosition = Vector3.zero;

		public float TimeSinceSeeingMonster = 0f;

		public float TimeSinceHittingMonster = 0f;
	}
	[HarmonyPatch(typeof(NutcrackerEnemyAI))]
	internal class NutcrackerAIPatch
	{
		private static List<EnemyAI> enemyList = new List<EnemyAI>();

		private static Dictionary<NutcrackerEnemyAI, NutcrackerData> NutcrackerData = new Dictionary<NutcrackerEnemyAI, NutcrackerData>();

		private static bool enableNucracker = Script.BoundingConfig.enableNutcrackers.Value;

		private static bool debugSpam = Script.BoundingConfig.spammyLogs.Value;

		private static bool debugNutcrackers = Script.BoundingConfig.debugNutcrackers.Value;

		public static bool CheckLOSForMonsters(Vector3 monsterPosition, NutcrackerEnemyAI __instance, float width = 45f, int range = 60, int proximityAwareness = 60)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			if (Vector3.Distance(monsterPosition, ((EnemyAI)__instance).eye.position) < (float)range && !Physics.Linecast(((EnemyAI)__instance).eye.position, monsterPosition, StartOfRound.Instance.collidersAndRoomMaskAndDefault))
			{
				Vector3 val = monsterPosition - ((EnemyAI)__instance).eye.position;
				if (Vector3.Angle(((EnemyAI)__instance).eye.forward, val) < width || (proximityAwareness != -1 && Vector3.Distance(((EnemyAI)__instance).eye.position, monsterPosition) < (float)proximityAwareness))
				{
					return true;
				}
			}
			return false;
		}

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void UpdatePatch(NutcrackerEnemyAI __instance)
		{
			if (!NutcrackerData.ContainsKey(__instance))
			{
				NutcrackerData.Add(__instance, new NutcrackerData());
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void NutcrackerUpdatePostfix(NutcrackerEnemyAI __instance)
		{
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0330: Unknown result type (might be due to invalid IL or missing references)
			//IL_0335: Unknown result type (might be due to invalid IL or missing references)
			//IL_02df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0318: Unknown result type (might be due to invalid IL or missing references)
			//IL_031d: Unknown result type (might be due to invalid IL or missing references)
			//IL_027c: Unknown result type (might be due to invalid IL or missing references)
			//IL_028c: Unknown result type (might be due to invalid IL or missing references)
			//IL_029c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a1: Unknown result type (might be due to invalid IL or missing references)
			if (!enableNucracker)
			{
				return;
			}
			NutcrackerData nutcrackerData = NutcrackerData[__instance];
			enemyList = EnemyAIPatch.GetOutsideEnemyList(EnemyAIPatch.GetCompleteList((EnemyAI)(object)__instance), (EnemyAI)(object)__instance);
			nutcrackerData.closestEnemy = EnemyAIPatch.findClosestEnemy(enemyList, nutcrackerData.closestEnemy, (EnemyAI)(object)__instance);
			Vector3 velocity;
			if (((EnemyAI)__instance).currentBehaviourStateIndex == 1 && (Object)(object)nutcrackerData.closestEnemy != (Object)null)
			{
				if (__instance.isInspecting && CheckLOSForMonsters(((Component)nutcrackerData.closestEnemy).transform.position, __instance, 70f, 60, 1))
				{
					velocity = nutcrackerData.closestEnemy.agent.velocity;
					if (((Vector3)(ref velocity)).magnitude > 0f)
					{
						__instance.isInspecting = false;
						nutcrackerData.SeeMovingEnemy = true;
						__instance.lastSeenPlayerPos = ((Component)nutcrackerData.closestEnemy).transform.position;
					}
				}
			}
			else
			{
				if (((EnemyAI)__instance).currentBehaviourStateIndex != 2)
				{
					return;
				}
				if (nutcrackerData.SeeMovingEnemy)
				{
					__instance.StopInspection();
				}
				((EnemyAI)__instance).SwitchToBehaviourState(2);
				if (__instance.lostPlayerInChase)
				{
					__instance.targetTorsoDegrees = 0;
				}
				else
				{
					__instance.SetTargetDegreesToPosition(nutcrackerData.lastSeenEnemyPosition);
				}
				if (!((Object)(object)nutcrackerData.targetEnemy != (Object)null))
				{
					return;
				}
				if (((EnemyAI)__instance).CheckLineOfSightForPosition(((Component)nutcrackerData.targetEnemy).transform.position, 70f, 60, 1f, (Transform)null))
				{
					nutcrackerData.TimeSinceSeeingMonster = 0f;
					nutcrackerData.lastSeenEnemyPosition = ((Component)nutcrackerData.targetEnemy).transform.position;
					((EnemyAI)__instance).creatureAnimator.SetBool("AimDown", Vector3.Distance(nutcrackerData.lastSeenEnemyPosition, ((Component)__instance).transform.position) < 2f && nutcrackerData.lastSeenEnemyPosition.y < 1f);
				}
				if (!((EnemyAI)__instance).CheckLineOfSightForPosition(((Component)nutcrackerData.targetEnemy).transform.position, 70f, 25, 1f, (Transform)null))
				{
					return;
				}
				if (Object.op_Implicit((Object)(object)nutcrackerData.targetEnemy) && nutcrackerData.TimeSinceSeeingMonster < 8f && __instance.timeSinceSeeingTarget < 8f)
				{
					if (__instance.timeSinceFiringGun > 0.75f && !__instance.reloadingGun && !__instance.aimingGun && nutcrackerData.TimeSinceHittingMonster > 1f && Vector3.Angle(__instance.gun.shotgunRayPoint.forward, ((Component)nutcrackerData.targetEnemy).transform.position - __instance.gun.shotgunRayPoint.position) < 30f)
					{
						__instance.timeSinceFiringGun = 0f;
						((EnemyAI)__instance).agent.speed = 0f;
						__instance.AimGunServerRpc(((Component)__instance).transform.position);
					}
					if (__instance.lostPlayerInChase)
					{
						__instance.SetLostPlayerInChaseServerRpc(false);
					}
					nutcrackerData.TimeSinceSeeingMonster = 0f;
					nutcrackerData.lastSeenEnemyPosition = ((Component)nutcrackerData.targetEnemy).transform.position;
				}
				else
				{
					velocity = nutcrackerData.targetEnemy.agent.velocity;
					if (((Vector3)(ref velocity)).magnitude > 0f)
					{
						nutcrackerData.TimeSinceSeeingMonster = 0f;
					}
				}
			}
		}

		[HarmonyPatch("DoAIInterval")]
		[HarmonyPostfix]
		private static void DoAIIntervalPatch(NutcrackerEnemyAI __instance)
		{
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			if (!enableNucracker)
			{
				return;
			}
			NutcrackerData nutcrackerData = NutcrackerData[__instance];
			if (((EnemyAI)__instance).currentBehaviourStateIndex != 2 || !((Object)(object)nutcrackerData.targetEnemy != (Object)null))
			{
				return;
			}
			if (nutcrackerData.TimeSinceSeeingMonster < 0.5f && __instance.timeSinceSeeingTarget < 0.5f)
			{
				if (__instance.attackSearch.inProgress)
				{
					((EnemyAI)__instance).StopSearch(__instance.attackSearch, true);
				}
				__instance.reachedStrafePosition = false;
				((EnemyAI)__instance).SetDestinationToPosition(((Component)nutcrackerData.targetEnemy).transform.position, false);
				((EnemyAI)__instance).agent.stoppingDistance = 1f;
				((EnemyAI)__instance).moveTowardsDestination = true;
			}
			if (nutcrackerData.TimeSinceSeeingMonster > 12f && __instance.timeSinceSeeingTarget > 12f)
			{
				((EnemyAI)__instance).SwitchToBehaviourState(1);
			}
		}
	}
	internal class PufferData
	{
		public int reactionToHit = 0;

		public EnemyAI? targetEnemy = null;
	}
	[HarmonyPatch(typeof(PufferAI))]
	internal class PufferAIPatch
	{
		private static bool enableSporeLizard = Script.BoundingConfig.enableSporeLizard.Value;

		private static Dictionary<PufferAI, PufferData> pufferList = new Dictionary<PufferAI, PufferData>();

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPostfix(PufferAI __instance)
		{
			if (!pufferList.ContainsKey(__instance))
			{
				pufferList.Add(__instance, new PufferData());
			}
		}

		[HarmonyPatch("DoAIInterval")]
		[HarmonyPrefix]
		private static bool PrefixAIInterval(PufferAI __instance)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			PufferData pufferData = pufferList[__instance];
			if (((EnemyAI)__instance).currentBehaviourStateIndex == 2 && (Object)(object)pufferData.targetEnemy != (Object)null && Vector3.Distance(((Component)__instance.closestSeenPlayer).transform.position, ((Component)__instance).transform.position) < Vector3.Distance(((Component)pufferData.targetEnemy).transform.position, ((Component)__instance).transform.position))
			{
				if (((EnemyAI)__instance).moveTowardsDestination)
				{
					((EnemyAI)__instance).agent.SetDestination(((EnemyAI)__instance).destination);
				}
				((EnemyAI)__instance).SyncPositionToClients();
				return false;
			}
			return true;
		}

		[HarmonyPatch("DoAIInterval")]
		[HarmonyPostfix]
		private static void PostfixAIInterval(PufferAI __instance)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			PufferData pufferData = pufferList[__instance];
			if (((EnemyAI)__instance).currentBehaviourStateIndex == 2 && (Object)(object)pufferData.targetEnemy != (Object)null && Vector3.Distance(((Component)__instance.closestSeenPlayer).transform.position, ((Component)__instance).transform.position) < Vector3.Distance(((Component)pufferData.targetEnemy).transform.position, ((Component)__instance).transform.position))
			{
				((EnemyAI)__instance).SetDestinationToPosition(((Component)pufferData.targetEnemy).transform.position, true);
			}
			else
			{
				pufferData.reactionToHit = 0;
			}
		}

		public static void CustomOnHit(int force, EnemyAI enemyWhoHit, bool playHitSFX, PufferAI instance)
		{
			if (enableSporeLizard)
			{
				PufferData pufferData = pufferList[instance];
				((EnemyAI)instance).creatureAnimator.SetBool("alerted", true);
				((EnemyAI)instance).enemyHP = ((EnemyAI)instance).enemyHP - force;
				Script.Logger.LogDebug((object)"SpodeLizard CustomHit Triggered");
				HitEnemyTest(force, enemyWhoHit, playHitSFX, instance);
				((EnemyAI)instance).SwitchToBehaviourState(2);
				if (((EnemyAI)instance).enemyHP <= 0)
				{
					((EnemyAI)instance).KillEnemy(true);
				}
			}
		}

		public static void HitEnemyTest(int force, EnemyAI enemyWhoHit, bool playHitSFX, PufferAI instance)
		{
			int num = EnemyAIPatch.ReactToHit(force);
			if (enemyWhoHit is SandSpiderAI)
			{
				pufferList[instance].reactionToHit = 2;
			}
			else
			{
				pufferList[instance].reactionToHit = 1;
			}
		}
	}
	internal class SpiderData
	{
		public EnemyAI? closestEnemy = null;

		public EnemyAI? targetEnemy = null;

		public List<EnemyAI> enemyList = new List<EnemyAI>();

		public List<EnemyAI> knownEnemy = new List<EnemyAI>();

		public List<EnemyAI> deadEnemyBodies = new List<EnemyAI>();

		public float LookAtEnemyTimer = 0f;

		public Dictionary<EnemyAI, float> enemiesInLOSDictionary = new Dictionary<EnemyAI, float>();
	}
	[HarmonyPatch]
	internal class Reversepatch
	{
		[HarmonyReversePatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPatch(typeof(EnemyAI), "Update")]
		public static void ReverseUpdate(SandSpiderAI instance)
		{
		}
	}
	[HarmonyPatch(typeof(SandSpiderAI))]
	internal class SandSpiderAIPatch
	{
		private static float refreshCDtimeSpider = 1f;

		private static bool enableSpider = Script.BoundingConfig.enableSpider.Value;

		private static bool spiderHuntHoardingbug = Script.BoundingConfig.spiderHuntHoardingbug.Value;

		private static Dictionary<SandSpiderAI, SpiderData> spiderList = new Dictionary<SandSpiderAI, SpiderData>();

		private static bool debugSpider = Script.BoundingConfig.debugSpiders.Value;

		private static bool debugSpam = Script.BoundingConfig.spammyLogs.Value;

		[HarmonyPatch("Start")]
		[HarmonyPrefix]
		private static void StartPatch(SandSpiderAI __instance)
		{
			if (!spiderList.ContainsKey(__instance))
			{
				spiderList.Add(__instance, new SpiderData());
				SpiderData spiderData = spiderList[__instance];
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static bool UpdatePrefixPatch(SandSpiderAI __instance)
		{
			//IL_031d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0460: Unknown result type (might be due to invalid IL or missing references)
			//IL_058b: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_04cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_04df: Unknown result type (might be due to invalid IL or missing references)
			//IL_05f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_060b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0616: Unknown result type (might be due to invalid IL or missing references)
			//IL_0626: Unknown result type (might be due to invalid IL or missing references)
			//IL_078f: Unknown result type (might be due to invalid IL or missing references)
			//IL_067a: Unknown result type (might be due to invalid IL or missing references)
			//IL_067f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0682: Unknown result type (might be due to invalid IL or missing references)
			//IL_06c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_06d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_089b: Unknown result type (might be due to invalid IL or missing references)
			//IL_08a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0743: Unknown result type (might be due to invalid IL or missing references)
			//IL_074e: Unknown result type (might be due to invalid IL or missing references)
			//IL_08c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_08cd: Unknown result type (might be due to invalid IL or missing references)
			if (!enableSpider)
			{
				return true;
			}
			SpiderData spiderData = spiderList[__instance];
			spiderData.enemyList = EnemyAIPatch.GetInsideEnemyList(EnemyAIPatch.GetCompleteList((EnemyAI)(object)__instance), (EnemyAI)(object)__instance);
			spiderData.enemiesInLOSDictionary = EnemyAIPatch.GetEnemiesInLOS((EnemyAI)(object)__instance, spiderData.enemyList, 80f, 15, 2f);
			if (spiderData.enemiesInLOSDictionary.Count > 0)
			{
				foreach (KeyValuePair<EnemyAI, float> item in spiderData.enemiesInLOSDictionary)
				{
					if (item.Key.isEnemyDead)
					{
						if (debugSpider)
						{
							Script.Logger.LogWarning((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Update Postfix: " + ((object)item.Key)?.ToString() + " is Dead! Checking deadEnemyBodies list and skipping..."));
						}
						if (!spiderData.deadEnemyBodies.Contains(item.Key))
						{
							spiderData.deadEnemyBodies.Add(item.Key);
							if (debugSpider)
							{
								Script.Logger.LogWarning((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Update Postfix: " + ((object)item.Key)?.ToString() + " added to deadEnemyBodies list"));
							}
						}
					}
					else if (spiderData.knownEnemy.Contains(item.Key))
					{
						if (debugSpider && debugSpam)
						{
							Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Update Postfix: " + ((object)item.Key)?.ToString() + " is already in knownEnemyList"));
						}
					}
					else
					{
						if (debugSpider)
						{
							Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Update Postfix: Adding " + ((object)item.Key)?.ToString() + " to knownEnemyList"));
						}
						spiderData.knownEnemy.Add(item.Key);
					}
				}
				for (int i = 0; i < spiderData.knownEnemy.Count; i++)
				{
					if (spiderData.knownEnemy[i].isEnemyDead)
					{
						if (debugSpider)
						{
							Script.Logger.LogWarning((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Update Postfix: Removed " + ((object)spiderData.knownEnemy[i])?.ToString() + " from knownEnemyList"));
						}
						spiderData.knownEnemy.Remove(spiderData.knownEnemy[i]);
					}
				}
			}
			__instance.SyncMeshContainerPositionToClients();
			__instance.CalculateMeshMovement();
			switch (((EnemyAI)__instance).currentBehaviourStateIndex)
			{
			case 0:
				spiderData.closestEnemy = EnemyAIPatch.findClosestEnemy(spiderData.knownEnemy, spiderData.closestEnemy, (EnemyAI)(object)__instance);
				if ((Object)(object)spiderData.closestEnemy != (Object)null && ((EnemyAI)__instance).CheckLineOfSightForPosition(((Component)spiderData.closestEnemy).transform.position, 80f, 15, 2f, ((EnemyAI)__instance).eye) && !spiderData.closestEnemy.isEnemyDead)
				{
					spiderData.targetEnemy = spiderData.closestEnemy;
					if (debugSpider)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case0/ Set " + ((object)spiderData.closestEnemy)?.ToString() + " as TargetEnemy"));
					}
					((EnemyAI)__instance).SwitchToBehaviourState(2);
					if (debugSpider)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case0/ Set state to " + ((EnemyAI)__instance).currentBehaviourStateIndex));
					}
					__instance.chaseTimer = 12.5f;
					__instance.watchFromDistance = Vector3.Distance(((Component)__instance.meshContainer).transform.position, ((Component)spiderData.closestEnemy).transform.position) > 8f;
				}
				break;
			case 2:
				if ((Object)(object)((EnemyAI)__instance).targetPlayer != (Object)null)
				{
					break;
				}
				if ((Object)(object)spiderData.targetEnemy != (Object)(object)spiderData.closestEnemy && (Object)(object)spiderData.closestEnemy != (Object)null && ((EnemyAI)__instance).CheckLineOfSightForPosition(((Component)spiderData.closestEnemy).transform.position, 80f, 15, 2f, ((EnemyAI)__instance).eye))
				{
					if (spiderData.targetEnemy is HoarderBugAI && !(spiderData.closestEnemy is HoarderBugAI) && Vector3.Distance(__instance.meshContainer.position, ((Component)spiderData.targetEnemy).transform.position) * 1.2f < Vector3.Distance(__instance.meshContainer.position, ((Component)spiderData.closestEnemy).transform.position))
					{
						spiderData.targetEnemy = spiderData.closestEnemy;
					}
					else
					{
						spiderData.targetEnemy = spiderData.closestEnemy;
					}
				}
				if ((Object)(object)spiderData.targetEnemy == (Object)null)
				{
					if (debugSpider)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case2-0/ Stopping chasing: " + (object)spiderData.targetEnemy));
					}
					spiderData.targetEnemy = null;
					__instance.StopChasing(false);
					break;
				}
				if (__instance.onWall)
				{
					((EnemyAI)__instance).SetDestinationToPosition(((Component)spiderData.targetEnemy).transform.position, false);
					((EnemyAI)__instance).agent.speed = 4.25f;
					__instance.spiderSpeed = 4.25f;
					if (debugSpider)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case2/ onWall"));
					}
					break;
				}
				if (__instance.watchFromDistance && (Object)(object)((EnemyAI)__instance).GetClosestPlayer(true, false, false) != (Object)null && Vector3.Distance(__instance.meshContainerPosition, ((Component)((EnemyAI)__instance).GetClosestPlayer(true, false, false)).transform.position) > Vector3.Distance(__instance.meshContainerPosition, ((Component)spiderData.targetEnemy).transform.position))
				{
					if (spiderData.LookAtEnemyTimer <= 0f)
					{
						spiderData.LookAtEnemyTimer = 3f;
						((EnemyAI)__instance).movingTowardsTargetPlayer = false;
						__instance.overrideSpiderLookRotation = true;
						Vector3 position = ((Component)spiderData.targetEnemy).transform.position;
						__instance.SetSpiderLookAtPosition(position);
					}
					else
					{
						spiderData.LookAtEnemyTimer -= Time.deltaTime;
					}
					__instance.spiderSpeed = 0f;
					((EnemyAI)__instance).agent.speed = 0f;
					if (Physics.Linecast(__instance.meshContainer.position, ((Component)spiderData.targetEnemy).transform.position, StartOfRound.Instance.collidersAndRoomMaskAndDefault))
					{
						if (debugSpider)
						{
							Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case2-1/ Stopping chasing: " + (object)spiderData.targetEnemy));
						}
						spiderData.targetEnemy = null;
						__instance.StopChasing(false);
					}
					else if (Vector3.Distance(((Component)spiderData.targetEnemy).transform.position, __instance.meshContainer.position) < 5f || ((EnemyAI)__instance).stunNormalizedTimer > 0f)
					{
						__instance.watchFromDistance = false;
					}
					break;
				}
				((EnemyAI)__instance).SetDestinationToPosition(((Component)spiderData.targetEnemy).transform.position, false);
				if ((Object)(object)spiderData.targetEnemy == (Object)null || spiderData.targetEnemy.isEnemyDead)
				{
					if (debugSpider)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case2-2/ Stopping chasing: " + (object)spiderData.targetEnemy));
					}
					if ((Object)(object)spiderData.targetEnemy != (Object)null)
					{
						try
						{
							spiderData.deadEnemyBodies.Add(spiderData.targetEnemy);
							spiderData.knownEnemy.Remove(spiderData.targetEnemy);
							if (debugSpider)
							{
								Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case2-2/ Moved dead enemy to separate list"));
							}
						}
						catch
						{
							Script.Logger.LogError((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case2-2/ Enemy does not exist!"));
						}
					}
					spiderData.targetEnemy = null;
					__instance.StopChasing(false);
				}
				else
				{
					if (!(Vector3.Distance(((Component)spiderData.targetEnemy).transform.position, __instance.homeNode.position) > 12f) || !(Vector3.Distance(((Component)spiderData.targetEnemy).transform.position, __instance.meshContainer.position) > 5f))
					{
						break;
					}
					__instance.chaseTimer -= Time.deltaTime;
					if (__instance.chaseTimer <= 0f)
					{
						if (debugSpider)
						{
							Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "Update Postfix: /case2-3/ Stopping chasing: " + (object)spiderData.targetEnemy));
						}
						spiderData.targetEnemy = null;
						__instance.StopChasing(false);
					}
				}
				break;
			}
			if (refreshCDtimeSpider <= 0f)
			{
				if (debugSpider && debugSpam)
				{
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "watchFromDistance: " + __instance.watchFromDistance));
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "overrideSpiderLookRotation: " + __instance.overrideSpiderLookRotation));
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "moveTowardsDestination: " + ((EnemyAI)__instance).moveTowardsDestination));
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "movingTowardsTargetPlayer: " + ((EnemyAI)__instance).movingTowardsTargetPlayer));
				}
				refreshCDtimeSpider = 0.5f;
			}
			else
			{
				refreshCDtimeSpider -= Time.deltaTime;
			}
			if ((Object)(object)spiderData.targetEnemy != (Object)null && !Object.op_Implicit((Object)(object)((EnemyAI)__instance).targetPlayer) && ((EnemyAI)__instance).currentBehaviourStateIndex == 2)
			{
				Reversepatch.ReverseUpdate(__instance);
				if (((EnemyAI)__instance).updateDestinationInterval >= 0f)
				{
					((EnemyAI)__instance).updateDestinationInterval = ((EnemyAI)__instance).updateDestinationInterval - Time.deltaTime;
				}
				else
				{
					((EnemyAI)__instance).updateDestinationInterval = ((EnemyAI)__instance).AIIntervalTime + Random.Range(-0.015f, 0.015f);
					((EnemyAI)__instance).DoAIInterval();
				}
				__instance.timeSinceHittingPlayer += Time.deltaTime;
				return false;
			}
			return true;
		}

		[HarmonyPatch("DoAIInterval")]
		[HarmonyPrefix]
		private static bool DoAIIntervalPrefix(SandSpiderAI __instance)
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			if (!spiderHuntHoardingbug)
			{
				return true;
			}
			SpiderData spiderData = spiderList[__instance];
			if ((Object)(object)spiderData.targetEnemy != (Object)null && !Object.op_Implicit((Object)(object)((EnemyAI)__instance).targetPlayer) && ((EnemyAI)__instance).currentBehaviourStateIndex == 2)
			{
				if (((EnemyAI)__instance).moveTowardsDestination)
				{
					((EnemyAI)__instance).agent.SetDestination(((EnemyAI)__instance).destination);
				}
				((EnemyAI)__instance).SyncPositionToClients();
				if (debugSpider && debugSpam)
				{
					Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval Prefix: false"));
				}
				return false;
			}
			if (debugSpider && debugSpam)
			{
				Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval Prefix: true"));
			}
			return true;
		}

		[HarmonyPatch("DoAIInterval")]
		[HarmonyPostfix]
		private static void DoAIIntervalPostfix(SandSpiderAI __instance)
		{
			//IL_02e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
			SpiderData spiderData = spiderList[__instance];
			switch (((EnemyAI)__instance).currentBehaviourStateIndex)
			{
			case 0:
				if (debugSpider && debugSpam)
				{
					Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval Postfix: /case0/ nothing"));
				}
				break;
			case 1:
			{
				if (debugSpider && debugSpam)
				{
					Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval Postfix: /case1/"));
				}
				List<EnemyAI> list = spiderData.enemiesInLOSDictionary.Keys.ToList();
				if (__instance.reachedWallPosition)
				{
					for (int i = 0; i < list.Count; i++)
					{
						if (Vector3.Distance(__instance.meshContainer.position, ((Component)list[i]).transform.position) < 5f || list[i] is HoarderBugAI)
						{
							ChaseEnemy(__instance, list[i]);
							if (debugSpider)
							{
								Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval Postfix: /case1/ Chasing enemy: " + (object)list[i]));
							}
							break;
						}
						if (Vector3.Distance(__instance.meshContainer.position, ((Component)list[i]).transform.position) < 10f)
						{
							Vector3 position = ((Component)list[i]).transform.position;
							float num = Vector3.Dot(position - __instance.meshContainer.position, __instance.wallNormal);
							Vector3 val = position - num * __instance.wallNormal;
							__instance.meshContainerTargetRotation = Quaternion.LookRotation(val, __instance.wallNormal);
							__instance.overrideSpiderLookRotation = true;
							if (debugSpider)
							{
								Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval Postfix: /case1/ Moving off-wall to enemy: " + (object)list[i]));
							}
							break;
						}
					}
				}
				__instance.overrideSpiderLookRotation = false;
				break;
			}
			case 2:
				if (!((Object)(object)spiderData.targetEnemy != (Object)null))
				{
					break;
				}
				if (spiderData.targetEnemy.isEnemyDead)
				{
					if (debugSpider)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval Postfix: /case2/ Stopping chasing: " + (object)spiderData.targetEnemy));
					}
					spiderData.targetEnemy = null;
					__instance.StopChasing(false);
				}
				else if (__instance.watchFromDistance)
				{
					((EnemyAI)__instance).SetDestinationToPosition(((Component)((EnemyAI)__instance).ChooseClosestNodeToPosition(((Component)spiderData.targetEnemy).transform.position, false, 4)).transform.position, false);
					if (debugSpider)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval Postfix: /case2/ Set destination to: " + (object)spiderData.targetEnemy));
					}
				}
				break;
			}
		}

		private static void ChaseEnemy(SandSpiderAI ins, EnemyAI target, SandSpiderWebTrap? triggeredWeb = null)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			SpiderData spiderData = spiderList[ins];
			if ((((EnemyAI)ins).currentBehaviourStateIndex != 2 && ins.watchFromDistance) || Vector3.Distance(((Component)target).transform.position, ins.homeNode.position) < 25f || Vector3.Distance(ins.meshContainer.position, ((Component)target).transform.position) < 15f)
			{
				ins.watchFromDistance = false;
				spiderData.targetEnemy = target;
				ins.chaseTimer = 12.5f;
				((EnemyAI)ins).SwitchToBehaviourState(2);
				if (debugSpider)
				{
					Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)ins) + "ChaseEnemy: Switched state to: " + ((EnemyAI)ins).currentBehaviourStateIndex));
				}
			}
		}
	}
	internal class SpiderWebValues
	{
		public struct EnemyInfo
		{
			internal EnemyAI enemyAI { get; set; }

			internal float enterAgentSpeed { get; set; }

			internal float enterAnimationSpeed { get; set; }

			public EnemyInfo(EnemyAI enemy, float enterAgentSpeed, float enterAnimationSpeed)
			{
				enemyAI = enemy;
				this.enterAgentSpeed = enterAgentSpeed;
				this.enterAnimationSpeed = enterAnimationSpeed;
			}
		}

		public Dictionary<EnemyAI, EnemyInfo> collidingEnemy = new Dictionary<EnemyAI, EnemyInfo>();
	}
	[HarmonyPatch(typeof(SandSpiderWebTrap))]
	internal class SandSpiderWebTrapPatch
	{
		private static Dictionary<SandSpiderWebTrap, SpiderWebValues> spiderWebs = new Dictionary<SandSpiderWebTrap, SpiderWebValues>();

		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void AwakePostfix(SandSpiderWebTrap __instance)
		{
			if (!spiderWebs.ContainsKey(__instance))
			{
				spiderWebs.Add(__instance, new SpiderWebValues());
			}
		}

		[HarmonyPatch("OnTriggerStay")]
		[HarmonyPostfix]
		private static void OnTriggerStayPatch(Collider other, SandSpiderWebTrap __instance)
		{
			SpiderWebValues spiderWebValues = spiderWebs[__instance];
			EnemyAI mainScript = ((Component)other).gameObject.GetComponent<EnemyAICollisionDetect>().mainScript;
			if (Script.BoundingConfig.debugSpiders.Value)
			{
				Script.Logger.LogInfo((object)(((object)__instance)?.ToString() + " Triggered"));
			}
			if ((Object)(object)mainScript != (Object)null)
			{
				if (Script.BoundingConfig.debugSpiders.Value)
				{
					Script.Logger.LogInfo((object)(((object)__instance)?.ToString() + " Collided with " + (object)mainScript));
				}
				if (!spiderWebValues.collidingEnemy.ContainsKey(mainScript))
				{
					spiderWebValues.collidingEnemy.Add(mainScript, new SpiderWebValues.EnemyInfo(mainScript, mainScript.agent.speed, mainScript.creatureAnimator.speed));
					if (Script.BoundingConfig.debugSpiders.Value)
					{
						Script.Logger.LogInfo((object)(((object)__instance)?.ToString() + " Added " + ((object)mainScript)?.ToString() + " to collidingEnemy"));
					}
				}
				mainScript.agent.speed = spiderWebValues.collidingEnemy[mainScript].enterAgentSpeed / 3f;
				mainScript.creatureAnimator.speed = spiderWebValues.collidingEnemy[mainScript].enterAnimationSpeed / 3f;
				if (Script.BoundingConfig.debugSpiders.Value)
				{
					Script.Logger.LogInfo((object)(((object)__instance)?.ToString() + " Slowed down " + (object)mainScript));
				}
				if (!__instance.webAudio.isPlaying)
				{
					__instance.webAudio.Play();
					__instance.webAudio.PlayOneShot(__instance.mainScript.hitWebSFX);
				}
			}
			if ((Object)(object)mainScript == (Object)null)
			{
				spiderWebValues.collidingEnemy.Clear();
				if (Script.BoundingConfig.debugSpiders.Value)
				{
					Script.Logger.LogInfo((object)(((object)__instance)?.ToString() + " Cleared " + ((object)mainScript)?.ToString() + " from collidingEnemy"));
				}
				if (__instance.webAudio.isPlaying && (Object)(object)__instance.currentTrappedPlayer == (Object)null)
				{
					__instance.webAudio.Stop();
				}
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static bool UpdatePrefix(SandSpiderWebTrap __instance, out bool __state)
		{
			SpiderWebValues spiderWebValues = spiderWebs[__instance];
			if ((Object)(object)__instance.currentTrappedPlayer != (Object)null)
			{
				__state = false;
				return true;
			}
			if (spiderWebValues.collidingEnemy.Count > 0)
			{
				__state = true;
				return false;
			}
			__state = false;
			return true;
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePostfix(SandSpiderWebTrap __instance, bool __state)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			if (__state)
			{
				SpiderWebValues spiderWebValues = spiderWebs[__instance];
				if (spiderWebValues.collidingEnemy.Count > 0)
				{
					__instance.leftBone.LookAt(((Component)spiderWebValues.collidingEnemy.First().Key).transform.position);
					__instance.rightBone.LookAt(((Component)spiderWebValues.collidingEnemy.First().Key).transform.position);
				}
				else
				{
					__instance.leftBone.LookAt(__instance.centerOfWeb);
					__instance.rightBone.LookAt(__instance.centerOfWeb);
				}
				((Component)__instance).transform.localScale = Vector3.Lerp(((Component)__instance).transform.localScale, new Vector3(1f, 1f, __instance.zScale), 8f * Time.deltaTime);
			}
		}
	}
	internal class ExtendedSandWormAIData
	{
		public float refreshCDtime = 0.5f;

		public EnemyAI? closestEnemy = null;

		public EnemyAI? targetEnemy = null;

		public int targetingEntity = 0;
	}
	[HarmonyPatch]
	internal class ReversepatchWorm
	{
		[HarmonyReversePatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPatch(typeof(EnemyAI), "Update")]
		public static void WormReverseUpdate(SandWormAI instance)
		{
		}
	}
	[HarmonyPatch(typeof(SandWormAI))]
	internal class SandWormAIPatch
	{
		private static List<EnemyAI> enemyList = new List<EnemyAI>();

		private static List<Type> targetedTypes = new List<Type>();

		private static bool debugSandworm = Script.BoundingConfig.debugSandworms.Value;

		private static bool debugSpam = Script.BoundingConfig.spammyLogs.Value;

		private static bool triggerFlag = Script.BoundingConfig.debugTriggerFlags.Value;

		private static Dictionary<SandWormAI, ExtendedSandWormAIData> sandworms = new Dictionary<SandWormAI, ExtendedSandWormAIData>();

		[HarmonyPatch("Start")]
		[HarmonyPrefix]
		private static void SandWormStartPatch(SandWormAI __instance)
		{
			if (!sandworms.ContainsKey(__instance))
			{
				sandworms.Add(__instance, new ExtendedSandWormAIData());
				if (targetedTypes.Count == 0)
				{
					targetedTypes.Add(typeof(BaboonBirdAI));
					targetedTypes.Add(typeof(ForestGiantAI));
					targetedTypes.Add(typeof(MouthDogAI));
					targetedTypes.Add(typeof(BushWolfEnemy));
					targetedTypes.Add(typeof(RadMechAI));
				}
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static bool SandWormPrefixUpdatePatch(SandWormAI __instance)
		{
			ExtendedSandWormAIData extendedSandWormAIData = sandworms[__instance];
			if (extendedSandWormAIData.refreshCDtime <= 0f)
			{
				extendedSandWormAIData.closestEnemy = EnemyAIPatch.findClosestEnemy(enemyList, extendedSandWormAIData.closestEnemy, (EnemyAI)(object)__instance);
				enemyList = EnemyAIPatch.filterEnemyList(EnemyAIPatch.GetOutsideEnemyList(EnemyAIPatch.GetCompleteList((EnemyAI)(object)__instance), (EnemyAI)(object)__instance), targetedTypes, (EnemyAI)(object)__instance);
				extendedSandWormAIData.refreshCDtime = 0.2f;
			}
			if (extendedSandWormAIData.refreshCDtime > 0f)
			{
				extendedSandWormAIData.refreshCDtime -= Time.deltaTime;
			}
			switch (extendedSandWormAIData.targetingEntity)
			{
			case 0:
				if (__instance.inEmergingState || __instance.emerged)
				{
					return false;
				}
				break;
			case 1:
				if (!((Object)(object)extendedSandWormAIData.targetEnemy != (Object)null) || ((EnemyAI)__instance).movingTowardsTargetPlayer)
				{
					break;
				}
				if (((EnemyAI)__instance).updateDestinationInterval >= 0f)
				{
					((EnemyAI)__instance).updateDestinationInterval = ((EnemyAI)__instance).updateDestinationInterval - Time.deltaTime;
				}
				else
				{
					if (debugSandworm && debugSpam)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " calling DoAIInterval"));
					}
					((EnemyAI)__instance).DoAIInterval();
					((EnemyAI)__instance).updateDestinationInterval = ((EnemyAI)__instance).AIIntervalTime + Random.Range(-0.015f, 0.015f);
				}
				return false;
			}
			if (debugSandworm && triggerFlag)
			{
				Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Prefix/2/ targetEnemy: " + ((object)extendedSandWormAIData.targetEnemy)?.ToString() + ", target: " + (object)extendedSandWormAIData.targetEnemy));
			}
			return true;
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void SandWormPostfixUpdatePatch(SandWormAI __instance)
		{
			//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			ExtendedSandWormAIData extendedSandWormAIData = sandworms[__instance];
			if (!Script.BoundingConfig.enableLeviathan.Value)
			{
				return;
			}
			if (debugSandworm && debugSpam)
			{
				Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Postfix targetingEntity: " + extendedSandWormAIData.targetingEntity));
			}
			switch (extendedSandWormAIData.targetingEntity)
			{
			case 0:
				if (!((EnemyAI)__instance).movingTowardsTargetPlayer && !__instance.inEmergingState && !__instance.emerged && ((EnemyAI)__instance).creatureSFX.isPlaying)
				{
					((EnemyAI)__instance).creatureSFX.Stop();
					if (debugSandworm)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " stopping Sounds"));
					}
				}
				break;
			case 1:
				if (((EnemyAI)__instance).movingTowardsTargetPlayer)
				{
					break;
				}
				if (!((EnemyAI)__instance).creatureSFX.isPlaying && !__instance.inEmergingState && !__instance.emerged)
				{
					int num = Random.Range(0, __instance.ambientRumbleSFX.Length);
					((EnemyAI)__instance).creatureSFX.clip = __instance.ambientRumbleSFX[num];
					((EnemyAI)__instance).creatureSFX.Play();
					if (debugSandworm)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Started playing sounds"));
					}
				}
				if (!((NetworkBehaviour)__instance).IsOwner)
				{
					break;
				}
				if (!Object.op_Implicit((Object)(object)extendedSandWormAIData.targetEnemy))
				{
					if (debugSandworm)
					{
						Script.Logger.LogError((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " TargetEnemy is null! TargetingEntity set to false /Trigger 1/"));
					}
					extendedSandWormAIData.targetingEntity = 0;
					break;
				}
				if (Vector3.Distance(((Component)extendedSandWormAIData.targetEnemy).transform.position, ((Component)__instance).transform.position) > 22f)
				{
					__instance.chaseTimer += Time.deltaTime;
					if (debugSandworm && debugSpam)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " updated chaseTimer: " + __instance.chaseTimer));
					}
				}
				else
				{
					__instance.chaseTimer = 0f;
					if (debugSandworm)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Reset chasetimer"));
					}
				}
				if (__instance.chaseTimer > 6f)
				{
					if (debugSandworm)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + " Chasing for too long. targetEnemy set to null"));
					}
					extendedSandWormAIData.targetingEntity = 0;
					extendedSandWormAIData.targetEnemy = null;
				}
				break;
			}
		}

		[HarmonyPatch("DoAIInterval")]
		[HarmonyPrefix]
		private static bool SandWormDoAIIntervalPrefix(SandWormAI __instance)
		{
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Unknown result type (might be due to invalid IL or missing references)
			//IL_0297: Unknown result type (might be due to invalid IL or missing references)
			//IL_043a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0308: Unknown result type (might be due to invalid IL or missing references)
			//IL_036a: Unknown result type (might be due to invalid IL or missing references)
			//IL_037a: Unknown result type (might be due to invalid IL or missing references)
			//IL_039b: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ab: Unknown result type (might be due to invalid IL or missing references)
			ExtendedSandWormAIData extendedSandWormAIData = sandworms[__instance];
			if (debugSandworm && debugSpam && triggerFlag)
			{
				Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: checking chaseTimer: " + __instance.chaseTimer));
			}
			switch (extendedSandWormAIData.targetingEntity)
			{
			case 0:
				if (__instance.emerged || __instance.inEmergingState)
				{
					break;
				}
				extendedSandWormAIData.closestEnemy = EnemyAIPatch.findClosestEnemy(enemyList, extendedSandWormAIData.closestEnemy, (EnemyAI)(object)__instance);
				((EnemyAI)__instance).agent.speed = 4f;
				if (debugSandworm)
				{
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: assigned " + ((object)extendedSandWormAIData.closestEnemy)?.ToString() + " as closestEnemy"));
				}
				if ((Object)(object)extendedSandWormAIData.closestEnemy != (Object)null && Vector3.Distance(((Component)__instance).transform.position, ((Component)extendedSandWormAIData.closestEnemy).transform.position) < 15f)
				{
					((EnemyAI)__instance).SetDestinationToPosition(((Component)extendedSandWormAIData.closestEnemy).transform.position, false);
					extendedSandWormAIData.targetingEntity = 1;
					extendedSandWormAIData.targetEnemy = extendedSandWormAIData.closestEnemy;
					__instance.chaseTimer = 0f;
					if (debugSandworm)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: Set targetingEntity to " + extendedSandWormAIData.targetingEntity + " and reset chaseTimer: " + __instance.chaseTimer));
					}
				}
				break;
			case 1:
				if ((Object)(object)extendedSandWormAIData.targetEnemy == (Object)null || extendedSandWormAIData.targetEnemy.isEnemyDead)
				{
					if (debugSandworm)
					{
						Script.Logger.LogError((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + ": targetEnemy is at null or dead. Setting targetingEntity to false /Trigger 2/"));
					}
					extendedSandWormAIData.targetEnemy = null;
					extendedSandWormAIData.targetingEntity = 0;
					break;
				}
				extendedSandWormAIData.targetEnemy = extendedSandWormAIData.closestEnemy;
				if (debugSandworm)
				{
					Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: Set " + ((object)extendedSandWormAIData.targetEnemy)?.ToString() + " as targetEnemy"));
				}
				if (!((Object)(object)extendedSandWormAIData.targetEnemy != (Object)null))
				{
					break;
				}
				if (Vector3.Distance(((Component)__instance).transform.position, ((Component)extendedSandWormAIData.targetEnemy).transform.position) > 19f)
				{
					extendedSandWormAIData.targetEnemy = null;
					if (debugSandworm)
					{
						Script.Logger.LogInfo((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: TargetEnemy too far! set to null"));
					}
					break;
				}
				if (!__instance.emerged && !__instance.inEmergingState)
				{
					((EnemyAI)__instance).SetDestinationToPosition(((Component)extendedSandWormAIData.targetEnemy).transform.position, true);
					if (debugSandworm && debugSpam)
					{
						Script.Logger.LogDebug((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: Set destitantion to " + (object)extendedSandWormAIData.targetEnemy));
					}
				}
				if (__instance.chaseTimer < 1.5f && Vector3.Distance(((Component)__instance).transform.position, ((Component)extendedSandWormAIData.targetEnemy).transform.position) < 4f && !(Vector3.Distance(StartOfRound.Instance.shipInnerRoomBounds.ClosestPoint(((Component)__instance).transform.position), ((Component)__instance).transform.position) < 9f) && Random.Range(0, 100) < 17)
				{
					if (debugSandworm)
					{
						Script.Logger.LogMessage((object)(EnemyAIPatch.DebugStringHead((EnemyAI?)(object)__instance) + "DoAIInterval: Emerging!"));
					}
					extendedSandWormAIData.targetingEntity = 0;
					__instance.StartEmergeAnimation();
				}
				break;
			}
			if (!((EnemyAI)__instance).movingTowardsTargetPlayer && extendedSandWormAIData.targetingEntity == 1)
			{
				if (((EnemyAI)__instance).moveTowardsDestination)
				{
					((EnemyAI)__instance).agent.SetDestination(((EnemyAI)__instance).destination);
				}
				((EnemyAI)__instance).SyncPositionToClients();
				return false;
			}
			return true;
		}
	}
}