Decompiled source of SpiderPositionFix v1.5.3

DLLs/fandovec03.SpiderPositionFix.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using SpiderPositionFix.Patches;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("fandovec03.SpiderPositionFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.5.3.0")]
[assembly: AssemblyInformationalVersion("1.5.3+c3dd3e0aafc9beeedad5c5aaf52a780e98de10ce")]
[assembly: AssemblyProduct("SpiderPositionFix")]
[assembly: AssemblyTitle("fandovec03.SpiderPositionFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.5.3.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 SpiderPositionFix
{
	[BepInPlugin("fandovec03.SpiderPositionFix", "SpiderPositionFix", "1.5.3")]
	public class InitialScript : BaseUnityPlugin
	{
		public static AssetBundle SpiderAssets;

		public static InitialScript Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		internal static ConfigClass configSettings { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			configSettings = new ConfigClass(((BaseUnityPlugin)this).Config);
			Patch();
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			SpiderAssets = AssetBundle.LoadFromFile(Path.Combine(directoryName, "spideranimationfixbundle"));
			if ((Object)(object)SpiderAssets == (Object)null)
			{
				Logger.LogError((object)"Failed to load Assets");
			}
			Logger.LogInfo((object)"fandovec03.SpiderPositionFix v1.5.3 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("fandovec03.SpiderPositionFix");
			}
			Logger.LogDebug((object)"Patching spider position fix...");
			Harmony.PatchAll(typeof(SpiderPositionPatch));
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching spider position fix...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	internal class ConfigClass
	{
		public readonly ConfigEntry<bool> applyMask;

		public readonly ConfigEntry<bool> debugLogs;

		public readonly ConfigEntry<bool> debugVisuals;

		public ConfigClass(ConfigFile cfg)
		{
			cfg.SaveOnConfigSet = false;
			applyMask = cfg.Bind<bool>("Settings", "Apply changes to agent areaMask", true, "Apply the changes made to the spider agent areaMask. This will affect the pathfinding over offMeshLinks");
			debugLogs = cfg.Bind<bool>("Debug", "Debug logs", false, "Enable debug logs");
			debugVisuals = cfg.Bind<bool>("Debug", "Debug visuals", false, "Enable visual debug tools.");
			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();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "fandovec03.SpiderPositionFix";

		public const string PLUGIN_NAME = "SpiderPositionFix";

		public const string PLUGIN_VERSION = "1.5.3";
	}
}
namespace SpiderPositionFix.Patches
{
	internal class spiderPositionData
	{
		public int currentJumpMaskBit = 1;

		public bool startPatch;

		public bool applySpeedSlowdown;

		public float originalSpeed = 4.25f;

		public float offsetSpeed;

		public float reachedWallTimer;

		public float delayTimer;

		public int delayTimes;

		public bool reachTheWallFail;

		public Dictionary<int, GameObject> debugObjects = new Dictionary<int, GameObject>();

		public float time = 0.2f;

		public Vector3 originalWallPosition = Vector3.zero;

		public float invalidPositionTimer;

		public int faildetToGetPositionTimes;

		public Transform altWallPosForMesh = new Transform();
	}
	[HarmonyPatch(typeof(SandSpiderAI))]
	public class SpiderPositionPatch
	{
		private static bool debugLogs = InitialScript.configSettings.debugLogs.Value;

		private static bool debugVisals = InitialScript.configSettings.debugVisuals.Value;

		internal static Dictionary<SandSpiderAI, spiderPositionData> spiderData = new Dictionary<SandSpiderAI, spiderPositionData>();

		private static GameObject ballPrefab;

		private static Material whiteBall;

		private static Material redBall;

		private static Material blueBall;

		private static Material greenBall;

		private static Material yellowBall;

		public static Transform getWallPosTransform(SandSpiderAI instance)
		{
			string text = "";
			spiderPositionData spiderPositionData2 = GetSpiderData(instance);
			if (spiderPositionData2.faildetToGetPositionTimes > 10)
			{
				text = "homeNode instance.homeNode";
				spiderPositionData2.altWallPosForMesh = instance.homeNode;
			}
			else
			{
				text = "transform instance.transform";
				spiderPositionData2.altWallPosForMesh = ((Component)instance).transform;
			}
			if (debugLogs)
			{
				InitialScript.Logger.LogInfo((object)("Returning " + text));
			}
			return spiderPositionData2.altWallPosForMesh;
		}

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPostfix(SandSpiderAI __instance)
		{
			if ((Object)(object)InitialScript.SpiderAssets != (Object)null)
			{
				try
				{
					AnimatorOverrideController runtimeAnimatorController = InitialScript.SpiderAssets.LoadAsset<AnimatorOverrideController>("Assets/LethalCompany/CustomAnims/SandSpider/Spider Anim Override.overrideController");
					if (debugVisals)
					{
						try
						{
							ballPrefab = InitialScript.SpiderAssets.LoadAsset<GameObject>("Assets/LethalCompany/CustomAnims/SandSpider/WhiteBall.prefab");
							whiteBall = InitialScript.SpiderAssets.LoadAsset<Material>("Assets/LethalCompany/CustomAnims/SandSpider/WhiteBallMat.mat");
							redBall = InitialScript.SpiderAssets.LoadAsset<Material>("Assets/LethalCompany/CustomAnims/SandSpider/RedBallMat.mat");
							blueBall = InitialScript.SpiderAssets.LoadAsset<Material>("Assets/LethalCompany/CustomAnims/SandSpider/BlueBallMat.mat");
							greenBall = InitialScript.SpiderAssets.LoadAsset<Material>("Assets/LethalCompany/CustomAnims/SandSpider/GreenBallMat.mat");
							yellowBall = InitialScript.SpiderAssets.LoadAsset<Material>("Assets/LethalCompany/CustomAnims/SandSpider/YellowBallMat.mat");
						}
						catch (Exception ex)
						{
							InitialScript.Logger.LogWarning((object)"Failed to load visual debug asset");
							InitialScript.Logger.LogWarning((object)ex);
						}
					}
					((EnemyAI)__instance).creatureAnimator.runtimeAnimatorController = (RuntimeAnimatorController)(object)runtimeAnimatorController;
				}
				catch
				{
					InitialScript.Logger.LogError((object)"Failed to load OverrideController asset");
				}
			}
			GetSpiderData(__instance).startPatch = true;
			GetSpiderData(__instance).altWallPosForMesh = ((Component)__instance).transform;
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static void UpdatePrefix(SandSpiderAI __instance)
		{
			spiderPositionData spiderPositionData2 = GetSpiderData(__instance);
			if (((NetworkBehaviour)__instance).IsOwner)
			{
				if (spiderPositionData2.time > 0f)
				{
					spiderPositionData2.time -= Time.deltaTime;
				}
				if (__instance.reachedWallPosition && ((Behaviour)((EnemyAI)__instance).agent).enabled && ((EnemyAI)__instance).agent.avoidancePriority == 25)
				{
					((EnemyAI)__instance).agent.avoidancePriority = 99;
					((Behaviour)((EnemyAI)__instance).agent).enabled = false;
				}
				if (!((Behaviour)((EnemyAI)__instance).agent).enabled && (!__instance.onWall || __instance.waitOnWallTimer <= 0f) && ((EnemyAI)__instance).agent.avoidancePriority == 99)
				{
					((EnemyAI)__instance).agent.avoidancePriority = 25;
					((Behaviour)((EnemyAI)__instance).agent).enabled = true;
					_ = __instance.onWall;
				}
			}
			else if (spiderPositionData2.time <= 0f)
			{
				spiderPositionData2.time = 0f;
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePostfix(SandSpiderAI __instance)
		{
			spiderPositionData spiderPositionData2 = GetSpiderData(__instance);
			if (!((NetworkBehaviour)__instance).IsOwner)
			{
				return;
			}
			if (spiderPositionData2.invalidPositionTimer > 0f)
			{
				spiderPositionData2.invalidPositionTimer -= Time.deltaTime;
			}
			if (InitialScript.configSettings.applyMask.Value)
			{
				if (((EnemyAI)__instance).isOutside && spiderPositionData2.currentJumpMaskBit != 1)
				{
					ChangeJumpMask(__instance, ref spiderPositionData2.currentJumpMaskBit);
					spiderPositionData2.currentJumpMaskBit = 1;
				}
				else if (!((EnemyAI)__instance).isOutside && spiderPositionData2.currentJumpMaskBit != 0)
				{
					ChangeJumpMask(__instance, ref spiderPositionData2.currentJumpMaskBit);
					spiderPositionData2.currentJumpMaskBit = 0;
				}
			}
			if (__instance.watchFromDistance)
			{
				return;
			}
			if (!__instance.onWall)
			{
				__instance.spiderSpeed = ((EnemyAI)__instance).agent.speed;
				if (((EnemyAI)__instance).agent.isOnOffMeshLink)
				{
					((EnemyAI)__instance).agent.speed = spiderPositionData2.originalSpeed / 1.15f;
					if (debugLogs)
					{
						InitialScript.Logger.LogDebug((object)"On offMeshLink. Cutting speed");
					}
				}
			}
			else
			{
				__instance.spiderSpeed = 3.75f;
				if (__instance.onWall)
				{
					((EnemyAI)__instance).agent.speed = 0f;
				}
			}
			if (__instance.reachedWallPosition)
			{
				spiderPositionData2.reachTheWallFail = false;
			}
		}

		[HarmonyPatch("LateUpdate")]
		[HarmonyPostfix]
		private static void MeshContainerPositionFix(SandSpiderAI __instance)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: 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_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: 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_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: 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_01ec: Unknown result type (might be due to invalid IL or missing references)
			spiderPositionData spiderPositionData2 = GetSpiderData(__instance);
			if (((NetworkBehaviour)__instance).IsOwner)
			{
				if (!spiderPositionData2.startPatch)
				{
					return;
				}
				if (!__instance.onWall && !((EnemyAI)__instance).agent.isOnOffMeshLink && Vector3.Distance(__instance.meshContainer.position, ((Component)__instance).transform.position) > 0.35f)
				{
					Vector3 velocity = ((EnemyAI)__instance).agent.velocity;
					if (((Vector3)(ref velocity)).magnitude > 3f && ((EnemyAI)__instance).agent.speed > 0.5f)
					{
						Vector3 position = ((Component)__instance).transform.position;
						velocity = ((EnemyAI)__instance).agent.velocity;
						__instance.meshContainerTarget = position + ((Vector3)(ref velocity)).normalized * 1.25f;
					}
					else
					{
						__instance.meshContainerTarget = ((Component)__instance).transform.position + ((EnemyAI)__instance).agent.velocity * Time.deltaTime;
					}
				}
			}
			if (__instance.onWall || __instance.gotWallPositionInLOS || !debugVisals)
			{
				return;
			}
			foreach (GameObject item in spiderPositionData2.debugObjects.Values.ToList())
			{
				Object.Destroy((Object)(object)item);
			}
			spiderPositionData2.debugObjects.Clear();
			GameObject spawningPrefab = ballPrefab;
			InstantiateVisalTool(__instance, spawningPrefab, yellowBall, "path1 corner #1", -5, __instance.meshContainer.position + ((EnemyAI)__instance).agent.velocity * Time.deltaTime * -1f);
			InstantiateVisalTool(__instance, spawningPrefab, greenBall, "refVel", -4, __instance.meshContainer.position + __instance.refVel);
			InstantiateVisalTool(__instance, spawningPrefab, redBall, "meshContainerTarget", -3, __instance.meshContainerTarget);
			InstantiateVisalTool(__instance, spawningPrefab, blueBall, "meshContainerServerPosition", -2, __instance.meshContainerServerPosition);
		}

		[HarmonyPatch("CalculateMeshMovement")]
		[HarmonyPrefix]
		private static bool MeshMovementPatch(SandSpiderAI __instance)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			spiderPositionData spiderPositionData2 = GetSpiderData(__instance);
			if (__instance.lookingForWallPosition && __instance.gotWallPositionInLOS && !__instance.onWall)
			{
				float num = Vector3.Distance(((Component)__instance).transform.position, __instance.floorPosition);
				float num2 = Vector3.Distance(__instance.meshContainer.position, __instance.floorPosition);
				if (spiderPositionData2.delayTimer > 1f)
				{
					if (debugLogs)
					{
						InitialScript.Logger.LogDebug((object)("distanceFromFloorPosition: " + num));
						InitialScript.Logger.LogDebug((object)("distanceFromFloorPositionMesh: " + num2));
					}
					spiderPositionData2.delayTimer = 0f;
					spiderPositionData2.delayTimes++;
					if (spiderPositionData2.delayTimes >= 20)
					{
						InitialScript.Logger.LogWarning((object)(((object)__instance)?.ToString() + ", NWID " + ((NetworkBehaviour)__instance).NetworkObjectId + " failing to climb walls within set timer!"));
						spiderPositionData2.delayTimes = 0;
					}
				}
				else
				{
					spiderPositionData2.delayTimer += Time.deltaTime;
				}
				((EnemyAI)__instance).SetDestinationToPosition(__instance.floorPosition, false);
				__instance.CalculateSpiderPathToPosition();
				if (num < 0.7f && num2 < 0.7f)
				{
					__instance.onWall = true;
					spiderPositionData2.delayTimes = 0;
					return true;
				}
				return false;
			}
			return true;
		}

		[HarmonyPatch("CalculateMeshMovement")]
		[HarmonyPostfix]
		private static void MeshMovementPostfixPatch(SandSpiderAI __instance)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: 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_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: 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)
			//IL_01e8: 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_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: 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_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: 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_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			spiderPositionData spiderPositionData2 = GetSpiderData(__instance);
			if (__instance.onWall)
			{
				return;
			}
			if (((EnemyAI)__instance).agent.isOnOffMeshLink)
			{
				__instance.meshContainer.position = Vector3.Lerp(__instance.meshContainer.position, ((Component)((EnemyAI)__instance).agent).transform.position, Distance(Vector3.Distance(__instance.meshContainer.position, ((Component)__instance).transform.position), 0.5f));
				__instance.meshContainerPosition = __instance.meshContainer.position;
				Quaternion rotation = __instance.meshContainer.rotation;
				OffMeshLinkData currentOffMeshLinkData = ((EnemyAI)__instance).agent.currentOffMeshLinkData;
				__instance.meshContainerTargetRotation = Quaternion.Lerp(rotation, Quaternion.LookRotation(((OffMeshLinkData)(ref currentOffMeshLinkData)).endPos - __instance.meshContainer.position, Vector3.up), 0.75f);
				return;
			}
			Vector3 velocity;
			if (spiderPositionData2.time <= 0f && debugLogs)
			{
				ManualLogSource logger = InitialScript.Logger;
				velocity = ((EnemyAI)__instance).agent.velocity;
				logger.LogDebug((object)((Vector3)(ref velocity)).magnitude);
				spiderPositionData2.time = 0.4f;
			}
			velocity = ((EnemyAI)__instance).agent.velocity;
			if (((Vector3)(ref velocity)).magnitude > 3f && !__instance.overrideSpiderLookRotation)
			{
				velocity = ((EnemyAI)__instance).agent.velocity;
				__instance.meshContainerTargetRotation = Quaternion.LookRotation(((Vector3)(ref velocity)).normalized + ((Component)__instance.meshContainer).transform.forward * 0.02f, Vector3.up);
			}
			velocity = ((EnemyAI)__instance).agent.velocity;
			if (((Vector3)(ref velocity)).magnitude > 3f && ((EnemyAI)__instance).agent.speed > 0.5f)
			{
				Vector3 position = ((Component)__instance).transform.position;
				velocity = ((EnemyAI)__instance).agent.velocity;
				__instance.navigateToPositionTarget = position + ((Vector3)(ref velocity)).normalized * 1.25f;
			}
			else
			{
				__instance.navigateToPositionTarget = ((Component)__instance).transform.position + ((EnemyAI)__instance).agent.velocity;
			}
		}

		private static float Distance(float distance, float time)
		{
			return distance / time;
		}

		private static void ChangeJumpMask(SandSpiderAI __instance, ref int bit)
		{
			if ((Object)(object)__instance != (Object)null)
			{
				NavMeshAgent agent = ((EnemyAI)__instance).agent;
				agent.areaMask ^= 1 << NavMesh.GetAreaFromName("Jump");
				if (bit == 0)
				{
					bit = 1;
				}
				else if (bit == 1)
				{
					bit = 0;
				}
				if (debugLogs)
				{
					InitialScript.Logger.LogDebug((object)("Spider: Toggled mask bit to " + bit));
				}
			}
		}

		[HarmonyPatch("GetWallPositionForSpiderMesh")]
		[HarmonyPrefix]
		private static void GetWallPositionForSpiderMeshPrefix(SandSpiderAI __instance)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			__instance.floorPosition = Vector3.zero;
		}

		[HarmonyPatch("GetWallPositionForSpiderMesh")]
		[HarmonyPostfix]
		private static void GetWallPositionForSpiderMeshPatch(SandSpiderAI __instance, ref bool __result)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			//IL_004c: 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)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_09e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_09e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Invalid comparison between Unknown and I4
			//IL_062c: Unknown result type (might be due to invalid IL or missing references)
			//IL_063b: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: 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_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Invalid comparison between Unknown and I4
			//IL_0652: Unknown result type (might be due to invalid IL or missing references)
			//IL_065d: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: 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_01af: Invalid comparison between Unknown and I4
			//IL_0849: 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_01b9: Invalid comparison between Unknown and I4
			//IL_027c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0286: Unknown result type (might be due to invalid IL or missing references)
			//IL_0293: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_034e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0350: Unknown result type (might be due to invalid IL or missing references)
			//IL_0387: Unknown result type (might be due to invalid IL or missing references)
			//IL_038c: Unknown result type (might be due to invalid IL or missing references)
			//IL_038e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0393: Unknown result type (might be due to invalid IL or missing references)
			//IL_0394: Unknown result type (might be due to invalid IL or missing references)
			//IL_0396: 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_03a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a7: 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)
			//IL_03b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03de: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0405: Unknown result type (might be due to invalid IL or missing references)
			//IL_041f: Unknown result type (might be due to invalid IL or missing references)
			//IL_042a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0440: Unknown result type (might be due to invalid IL or missing references)
			//IL_0447: Unknown result type (might be due to invalid IL or missing references)
			//IL_045d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0465: Unknown result type (might be due to invalid IL or missing references)
			//IL_0370: Unknown result type (might be due to invalid IL or missing references)
			//IL_08e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_08fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0482: Unknown result type (might be due to invalid IL or missing references)
			//IL_0484: Unknown result type (might be due to invalid IL or missing references)
			//IL_0488: Unknown result type (might be due to invalid IL or missing references)
			//IL_048d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0491: Unknown result type (might be due to invalid IL or missing references)
			//IL_0498: Unknown result type (might be due to invalid IL or missing references)
			//IL_049f: Unknown result type (might be due to invalid IL or missing references)
			//IL_04be: Unknown result type (might be due to invalid IL or missing references)
			//IL_04c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_04c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_04c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_04ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_053a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0545: Unknown result type (might be due to invalid IL or missing references)
			//IL_0595: Unknown result type (might be due to invalid IL or missing references)
			//IL_059a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0575: Unknown result type (might be due to invalid IL or missing references)
			spiderPositionData spiderPositionData2 = GetSpiderData(__instance);
			NavMeshHit val = default(NavMeshHit);
			Vector3 val2 = __instance.wallPosition + __instance.wallNormal;
			Vector3 val3 = default(Vector3);
			((Vector3)(ref val3))..ctor(val2.x, __instance.wallPosition.y, val2.z);
			NavMeshPath val4 = new NavMeshPath();
			Vector3 point = ((RaycastHit)(ref __instance.rayHit)).point;
			List<int> list = new List<int>();
			if (debugLogs)
			{
				InitialScript.Logger.LogInfo((object)$"Test | WallPosition: {__instance.wallPosition}, unmodifiedWallPosition: {point}");
			}
			if (__instance.floorPosition == Vector3.zero || RoundManager.Instance.GetNavMeshPosition(__instance.floorPosition, val, 0.7f, -1) == __instance.floorPosition || !((EnemyAI)__instance).agent.CalculatePath(__instance.floorPosition, val4) || (int)val4.status == 1 || (int)val4.status == 2)
			{
				if (spiderPositionData2.invalidPositionTimer <= 0f)
				{
					InitialScript.Logger.LogWarning((object)"failed to get valid position for floorPosition.");
					spiderPositionData2.invalidPositionTimer = 5f;
				}
				Vector3 zero = Vector3.zero;
				for (int i = 0; i < 4; i++)
				{
					zero = Vector3.Lerp(point, val3, (float)(i + 1) / 4f);
					Vector3 val5 = Vector3.zero;
					RaycastHit val6 = default(RaycastHit);
					if (Physics.Raycast(zero, Vector3.down, ref val6, 20f, StartOfRound.Instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1))
					{
						val5 = ((RaycastHit)(ref val6)).point;
					}
					if (val5 == Vector3.zero || RoundManager.Instance.GetNavMeshPosition(val5, val, 0.7f, -1) == val5 || !((EnemyAI)__instance).agent.CalculatePath(val5, val4) || (int)val4.status == 1 || (int)val4.status == 2)
					{
						__result = false;
						spiderPositionData2.faildetToGetPositionTimes++;
						continue;
					}
					__instance.floorPosition = val5;
					spiderPositionData2.faildetToGetPositionTimes = 0;
					__result = true;
					spiderPositionData2.invalidPositionTimer = 0f;
					InitialScript.Logger.LogMessage((object)"Assigned new floor position.");
					break;
				}
			}
			val4.ClearCorners();
			foreach (GameObject item in spiderPositionData2.debugObjects.Values.ToList())
			{
				Object.Destroy((Object)(object)item);
			}
			spiderPositionData2.debugObjects.Clear();
			if (!debugVisals)
			{
				return;
			}
			Dictionary<int, List<Vector3>> dictionary = new Dictionary<int, List<Vector3>>();
			GameObject val7 = ballPrefab;
			Vector3 val8 = default(Vector3);
			((Vector3)(ref val8))..ctor(__instance.meshContainer.position.x, point.y, __instance.meshContainer.position.z);
			((Renderer)val7.GetComponentInChildren<MeshRenderer>()).SetMaterial(redBall);
			val7.GetComponent<ScanNodeProperties>().headerText = "projected WallPosition";
			float num = Vector3.Distance(__instance.meshContainer.position, val8);
			int num2 = (int)MathF.Round(num * 2f, 0);
			if (debugLogs)
			{
				InitialScript.Logger.LogInfo((object)$"Rounded up {num} to {num2}");
			}
			LineRenderer componentInChildren = val7.GetComponentInChildren<LineRenderer>();
			((Renderer)componentInChildren).SetMaterial(redBall);
			componentInChildren.useWorldSpace = true;
			if (debugLogs)
			{
				InitialScript.Logger.LogInfo((object)"projected wallPosition");
			}
			spiderPositionData2.debugObjects.Add(-1, Object.Instantiate<GameObject>(val7, val8, Quaternion.identity));
			if (debugLogs)
			{
				InitialScript.Logger.LogInfo((object)$"instantiated projectedWallPosition {val8}");
			}
			Vector3 val9 = Vector3.Project(__instance.meshContainer.position - point, val3 - point) + point;
			Vector3 val10 = default(Vector3);
			((Vector3)(ref val10))..ctor(val9.x, __instance.meshContainer.position.y, val9.z);
			dictionary.Add(0, new List<Vector3>(1) { __instance.meshContainer.position });
			dictionary.Add(1, new List<Vector3>(2)
			{
				point,
				((Component)__instance).transform.position
			});
			dictionary.Add(2, new List<Vector3>(2) { __instance.floorPosition, point });
			dictionary.Add(3, new List<Vector3>(2) { val3, point });
			dictionary.Add(4, new List<Vector3>(2) { val9, point });
			Vector3 val12 = default(Vector3);
			Ray val13 = default(Ray);
			RaycastHit val14 = default(RaycastHit);
			for (int j = 1; j < num2; j++)
			{
				float num3 = (float)j / (float)num2;
				Vector3 val11 = Vector3.Lerp(val10, val9, num3);
				((Vector3)(ref val12))..ctor(point.x, val11.y, point.z);
				((Renderer)val7.GetComponentInChildren<MeshRenderer>()).SetMaterial(whiteBall);
				((Ray)(ref val13))..ctor(val11, val12 - val11);
				if (!Physics.Raycast(val13, ref val14, 7f, StartOfRound.Instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1))
				{
					InitialScript.Logger.LogWarning((object)"Raycast failed to hit anything within set distance.");
					list.Add(j);
				}
				val7.GetComponent<ScanNodeProperties>().headerText = $"Generated lerpedVector {j}";
				dictionary.Add(4 + j, new List<Vector3>(2)
				{
					((Ray)(ref val13)).GetPoint(((RaycastHit)(ref val14)).distance - 0.2f),
					val11
				});
				if (debugLogs)
				{
					InitialScript.Logger.LogInfo((object)$"set wallVector[{j}] to {dictionary[4 + j][0]}");
				}
				if (dictionary[4 + j][0] == Vector3.zero)
				{
					InitialScript.Logger.LogWarning((object)"Invalid raycast position detected");
				}
			}
			if (debugLogs)
			{
				InitialScript.Logger.LogInfo((object)$"final splitNum count = {num2}");
				InitialScript.Logger.LogInfo((object)$"total count = {num2}");
			}
			for (int k = 0; k < 4 + num2; k++)
			{
				InitialScript.Logger.LogInfo((object)$"Processing wallVector[{k}] |{k}|");
				try
				{
					bool flag = !Physics.Linecast(__instance.floorPosition, dictionary[k][0], StartOfRound.Instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1) || !Physics.Linecast(val3, dictionary[k][0], StartOfRound.Instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1);
					switch (k)
					{
					case 0:
						((Renderer)val7.GetComponentInChildren<MeshRenderer>()).SetMaterial(greenBall);
						val7.GetComponent<ScanNodeProperties>().headerText = "meshContainer position";
						((Object)val7.gameObject).name = $"meshContainer position {k}";
						break;
					case 1:
						((Renderer)val7.GetComponentInChildren<MeshRenderer>()).SetMaterial(blueBall);
						val7.GetComponent<ScanNodeProperties>().headerText = "wall position";
						((Object)val7.gameObject).name = $"wall position {k}";
						break;
					case 2:
						((Renderer)val7.GetComponentInChildren<MeshRenderer>()).SetMaterial(yellowBall);
						val7.GetComponent<ScanNodeProperties>().headerText = "floor position";
						((Object)val7.gameObject).name = $"floor position {k}";
						break;
					case 3:
						((Renderer)val7.GetComponentInChildren<MeshRenderer>()).SetMaterial(blueBall);
						val7.GetComponent<ScanNodeProperties>().headerText = "projected normal position";
						((Object)val7.gameObject).name = $"projected normal position {k}";
						break;
					case 4:
						((Renderer)val7.GetComponentInChildren<MeshRenderer>()).SetMaterial(blueBall);
						val7.GetComponent<ScanNodeProperties>().headerText = "projected meshContainer position on Normal";
						((Object)val7.gameObject).name = $"projected meshContainer position on Normal {k}";
						break;
					default:
						((Renderer)val7.GetComponentInChildren<MeshRenderer>()).SetMaterial(whiteBall);
						val7.GetComponent<ScanNodeProperties>().headerText = $"Generated position {k}";
						((Object)val7.gameObject).name = $"Generated position {k}";
						break;
					}
					if (debugLogs)
					{
						InitialScript.Logger.LogInfo((object)$"Successfully spawned ball at {dictionary[k][0]} |{k}|");
					}
					if (k > 4 && flag)
					{
						((Renderer)val7.GetComponentInChildren<MeshRenderer>()).material = yellowBall;
					}
					if (k > 4 && list.Contains(k))
					{
						((Renderer)val7.GetComponentInChildren<MeshRenderer>()).material = redBall;
					}
					if (dictionary[k].Count > 1)
					{
						if (debugLogs)
						{
							InitialScript.Logger.LogInfo((object)"Found multiple vectors");
						}
						try
						{
							InitialScript.Logger.LogInfo((object)$"Setting rendered line for {k}, 0: {dictionary[k][0]}, 1: {dictionary[k][1]}");
							componentInChildren = val7.GetComponentInChildren<LineRenderer>();
							((Renderer)componentInChildren).material = ((Renderer)val7.GetComponentInChildren<MeshRenderer>()).sharedMaterial;
							componentInChildren.useWorldSpace = true;
							((Object)((Component)componentInChildren).gameObject).name = $"{((Object)val7.gameObject).name} {k}";
							InitialScript.Logger.LogInfo((object)$"Set name for {k}, 0: {((Object)((Component)componentInChildren).gameObject).name}, 1: {((Object)val7.gameObject).name}");
							SetRenderedLinePoints(dictionary[k].ToArray(), componentInChildren);
						}
						catch (Exception ex)
						{
							InitialScript.Logger.LogError((object)$"failed to spawn a ray |{k}|");
							InitialScript.Logger.LogError((object)ex);
						}
					}
					spiderPositionData2.debugObjects.Add(k, Object.Instantiate<GameObject>(val7, dictionary[k][0], Quaternion.identity));
				}
				catch (Exception ex2)
				{
					InitialScript.Logger.LogError((object)$"failed to spawn a ball |{k}|");
					InitialScript.Logger.LogError((object)ex2);
				}
			}
		}

		public static void SetRenderedLinePoints(Vector3[] positions, LineRenderer lr)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			lr.positionCount = positions.Length;
			for (int i = 0; i < lr.positionCount; i++)
			{
				if (lr.positionCount < 2)
				{
					break;
				}
				lr.SetPosition(i, positions[i]);
			}
		}

		public static void InstantiateVisalTool(SandSpiderAI __instance, GameObject spawningPrefab, Material material, string headerText, int index, Vector3 position)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			spiderPositionData spiderPositionData2 = GetSpiderData(__instance);
			((Renderer)spawningPrefab.GetComponentInChildren<MeshRenderer>()).material = material;
			spawningPrefab.GetComponent<ScanNodeProperties>().headerText = headerText;
			spiderPositionData2.debugObjects.Add(index, Object.Instantiate<GameObject>(spawningPrefab, position, Quaternion.identity));
		}

		private static spiderPositionData GetSpiderData(SandSpiderAI spider)
		{
			if (!spiderData.ContainsKey(spider))
			{
				spiderData.Add(spider, new spiderPositionData());
			}
			return spiderData[spider];
		}
	}
	internal class EnemyAIPatch
	{
		[HarmonyPatch(typeof(EnemyAI), "OnDestroy")]
		[HarmonyPostfix]
		public static void OnDestroyPatch(EnemyAI aI)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (aI is SandSpiderAI)
			{
				SpiderPositionPatch.spiderData.Remove((SandSpiderAI)aI);
				InitialScript.Logger.LogMessage((object)$"Cleared {aI.enemyType.enemyName} #{aI.thisEnemyIndex}'s data");
			}
		}
	}
}