Decompiled source of Unconfirmed v0.5.1

Unconfirmed.dll

Decompiled 2 weeks 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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Rendering;

[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("Unconfirmed")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.5.1.0")]
[assembly: AssemblyInformationalVersion("0.5.1")]
[assembly: AssemblyProduct("Unconfirmed")]
[assembly: AssemblyTitle("Unconfirmed")]
[assembly: AssemblyMetadata("AI_Assisted_Creation", "This assembly was partially or fully created with the assistance of Generative AI (e.g., Code Suggestions, Refactoring, Documentation Generation).")]
[assembly: AssemblyMetadata("AI_Model_Vendor", "DeepSeek")]
[assembly: AssemblyVersion("0.5.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 Unconfirmed
{
	[BepInPlugin("Kasablanca.Unconfirmed", "Unconfirmed", "0.5.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "Kasablanca.Unconfirmed";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<float> StartThreshold;

		internal static ConfigEntry<float> MoveThreshold;

		internal static ConfigEntry<float> StareMinDist;

		internal static ConfigEntry<float> StareMaxDist;

		internal static ConfigEntry<float> MoveMinDist;

		internal static ConfigEntry<float> MoveMaxDist;

		internal static ConfigEntry<float> StareDuration;

		internal static ConfigEntry<float> StareBreakTime;

		internal static ConfigEntry<float> MoveDuration;

		internal static ConfigEntry<float> WanderSpeed;

		internal static ConfigEntry<float> StrideLength;

		internal static ConfigEntry<float> VanishDelay;

		internal static ConfigEntry<float> BaseInterval;

		internal static ConfigEntry<float> MinInterval;

		internal static ConfigEntry<float> MaxViewAngle;

		internal static ConfigEntry<int> SpawnAttempts;

		private void Awake()
		{
			//IL_026d: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_0296: Expected O, but got Unknown
			//IL_0296: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable or disable the mod.");
			StartThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("General", "StartThreshold", 55f, "Insanity percentage (0-100) at which hallucinations begin.");
			MoveThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MoveThreshold", 85f, "Insanity percentage at which the figure starts wandering.");
			BaseInterval = ((BaseUnityPlugin)this).Config.Bind<float>("General", "BaseInterval", 40f, "Seconds between hallucinations at the start threshold.");
			MinInterval = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MinInterval", 12f, "Seconds between hallucinations at 100% insanity.");
			MaxViewAngle = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MaxViewAngle", 35f, "Max angle (degrees) the figure may deviate from your view direction.");
			SpawnAttempts = ((BaseUnityPlugin)this).Config.Bind<int>("General", "SpawnAttempts", 12, "How many positions to try before giving up (avoids spawning inside walls).");
			StareMinDist = ((BaseUnityPlugin)this).Config.Bind<float>("Stare", "MinDistance", 12f, "Stare phase: minimum spawn distance in meters.");
			StareMaxDist = ((BaseUnityPlugin)this).Config.Bind<float>("Stare", "MaxDistance", 18f, "Stare phase: maximum spawn distance in meters.");
			StareDuration = ((BaseUnityPlugin)this).Config.Bind<float>("Stare", "Duration", 6f, "How long the figure stays visible, in seconds.");
			StareBreakTime = ((BaseUnityPlugin)this).Config.Bind<float>("Stare", "LookBreakTime", 2.5f, "Seconds of direct eye contact before the figure vanishes.");
			MoveMinDist = ((BaseUnityPlugin)this).Config.Bind<float>("Wander", "MinDistance", 7f, "Wander phase: minimum spawn distance in meters.");
			MoveMaxDist = ((BaseUnityPlugin)this).Config.Bind<float>("Wander", "MaxDistance", 12f, "Wander phase: maximum spawn distance in meters.");
			MoveDuration = ((BaseUnityPlugin)this).Config.Bind<float>("Wander", "Duration", 9f, "Max seconds the figure keeps walking away before it waits for you to look elsewhere.");
			WanderSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Wander", "Speed", 1.3f, "Walking speed of the figure, in m/s.");
			StrideLength = ((BaseUnityPlugin)this).Config.Bind<float>("Wander", "StrideLength", 1.15f, "Ground distance covered by one full step cycle (two steps), in meters. Smaller = faster stepping at the same Speed.");
			VanishDelay = ((BaseUnityPlugin)this).Config.Bind<float>("Wander", "VanishDelay", 1.5f, "Seconds the figure keeps walking after leaving your sight before it vanishes.");
			new Harmony("Kasablanca.Unconfirmed").PatchAll(typeof(Plugin).Assembly);
			GameObject val = new GameObject("Unconfirmed_Director");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<Director>();
			Log.LogInfo((object)"Unconfirmed 0.5.0 loaded.");
		}
	}
	internal static class Sanity
	{
		private static bool _probed;

		private static PropertyInfo _prop;

		internal static float Get(PlayerControllerB local)
		{
			if (!_probed)
			{
				_probed = true;
				try
				{
					_prop = (AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly a) => a.GetName().Name == "InsanityMod")?.GetType("InsanityMod.Managers.InsanityManager"))?.GetProperty("Insanity", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
					if (_prop != null)
					{
						Plugin.Log.LogInfo((object)"InsanityMod detected, reading sanity from it.");
					}
				}
				catch (Exception ex)
				{
					Plugin.Log.LogWarning((object)("Could not read InsanityMod: " + ex.Message));
				}
			}
			if (_prop != null)
			{
				try
				{
					return (float)_prop.GetValue(null);
				}
				catch
				{
				}
			}
			float num = ((local.maxInsanityLevel <= 0f) ? 1f : local.maxInsanityLevel);
			return Mathf.Clamp01(local.insanityLevel / num) * 100f;
		}
	}
	public class Director : MonoBehaviour
	{
		private float _nextAt;

		private void Update()
		{
			if (!Plugin.Enabled.Value)
			{
				return;
			}
			GameNetworkManager instance = GameNetworkManager.Instance;
			PlayerControllerB val = (((Object)(object)instance == (Object)null) ? null : instance.localPlayerController);
			if (!((Object)(object)val == (Object)null) && !val.isPlayerDead && val.isInsideFactory)
			{
				float num = Sanity.Get(val);
				if (!(num < Plugin.StartThreshold.Value) && !(Time.time < _nextAt))
				{
					float num2 = Mathf.InverseLerp(Plugin.StartThreshold.Value, 100f, num);
					_nextAt = Time.time + Mathf.Lerp(Plugin.BaseInterval.Value, Plugin.MinInterval.Value, num2);
					Spawn(val, num >= Plugin.MoveThreshold.Value);
				}
			}
		}

		private static string PathFrom(Transform root, Transform t)
		{
			List<string> list = new List<string>();
			while ((Object)(object)t != (Object)null && (Object)(object)t != (Object)(object)root)
			{
				list.Insert(0, ((Object)t).name);
				t = t.parent;
			}
			return string.Join("/", list);
		}

		private static GameObject BuildPlayerClone(PlayerControllerB src, Vector3 pos, Quaternion rot, out SkinnedMeshRenderer body)
		{
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			body = null;
			SkinnedMeshRenderer thisPlayerModel = src.thisPlayerModel;
			if ((Object)(object)thisPlayerModel == (Object)null || (Object)(object)thisPlayerModel.rootBone == (Object)null)
			{
				Plugin.Log.LogWarning((object)"Source has no thisPlayerModel/rootBone.");
				return null;
			}
			Transform val = ((Component)thisPlayerModel).transform;
			while ((Object)(object)val != (Object)null && !thisPlayerModel.rootBone.IsChildOf(val))
			{
				val = val.parent;
			}
			if ((Object)(object)val == (Object)null || (Object)(object)val == (Object)(object)((Component)src).transform)
			{
				Plugin.Log.LogWarning((object)("Model root resolved to " + (((Object)(object)val == (Object)null) ? "null" : "player root") + "; aborting."));
				return null;
			}
			GameObject val2 = new GameObject("Unconfirmed_Figure");
			val2.SetActive(false);
			val2.transform.SetPositionAndRotation(pos, rot);
			GameObject val3 = Object.Instantiate<GameObject>(((Component)val).gameObject, val2.transform, false);
			val3.transform.localPosition = ((Component)src).transform.InverseTransformPoint(val.position);
			val3.transform.localRotation = Quaternion.Inverse(((Component)src).transform.rotation) * val.rotation;
			val3.SetActive(true);
			Joint[] componentsInChildren = val3.GetComponentsInChildren<Joint>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren[i]);
			}
			MonoBehaviour[] componentsInChildren2 = val3.GetComponentsInChildren<MonoBehaviour>(true);
			foreach (MonoBehaviour val4 in componentsInChildren2)
			{
				if (!(((object)val4).GetType().Namespace ?? "").StartsWith("UnityEngine.Animations.Rigging"))
				{
					try
					{
						Object.DestroyImmediate((Object)(object)val4);
					}
					catch
					{
					}
				}
			}
			Collider[] componentsInChildren3 = val3.GetComponentsInChildren<Collider>(true);
			for (int i = 0; i < componentsInChildren3.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren3[i]);
			}
			Rigidbody[] componentsInChildren4 = val3.GetComponentsInChildren<Rigidbody>(true);
			for (int i = 0; i < componentsInChildren4.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)componentsInChildren4[i]);
			}
			Behaviour[] componentsInChildren5 = val3.GetComponentsInChildren<Behaviour>(true);
			foreach (Behaviour val5 in componentsInChildren5)
			{
				Animator val6 = (Animator)(object)((val5 is Animator) ? val5 : null);
				if (val6 != null)
				{
					val6.applyRootMotion = false;
				}
				else
				{
					val5.enabled = false;
				}
			}
			Transform[] componentsInChildren6 = val3.GetComponentsInChildren<Transform>(true);
			for (int i = 0; i < componentsInChildren6.Length; i++)
			{
				((Component)componentsInChildren6[i]).gameObject.layer = 0;
			}
			string text = PathFrom(val, ((Component)thisPlayerModel).transform);
			object obj3;
			if (text.Length != 0)
			{
				Transform obj2 = val3.transform.Find(text);
				obj3 = ((obj2 != null) ? ((Component)obj2).GetComponent<SkinnedMeshRenderer>() : null);
			}
			else
			{
				obj3 = val3.GetComponent<SkinnedMeshRenderer>();
			}
			body = (SkinnedMeshRenderer)obj3;
			SkinnedMeshRenderer[] componentsInChildren7 = val3.GetComponentsInChildren<SkinnedMeshRenderer>(true);
			foreach (SkinnedMeshRenderer obj4 in componentsInChildren7)
			{
				((Renderer)obj4).enabled = (Object)(object)obj4 == (Object)(object)body;
			}
			if ((Object)(object)body != (Object)null)
			{
				((Component)body).gameObject.SetActive(true);
				((Renderer)body).shadowCastingMode = (ShadowCastingMode)1;
				body.updateWhenOffscreen = true;
			}
			val2.SetActive(true);
			if ((Object)(object)body == (Object)null)
			{
				Plugin.Log.LogWarning((object)("Figure body mesh not found at '" + text + "'."));
			}
			return val2;
		}

		private void Spawn(PlayerControllerB local, bool wander, bool forced = false)
		{
			//IL_00bf: 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_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: 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_036e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0373: Unknown result type (might be due to invalid IL or missing references)
			//IL_0375: 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_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: 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_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_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: 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_039d: Unknown result type (might be due to invalid IL or missing references)
			//IL_039f: 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_03a4: 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_03a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0231: Unknown result type (might be due to invalid IL or missing references)
			//IL_0248: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_024c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			//IL_025b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_0262: Unknown result type (might be due to invalid IL or missing references)
			//IL_0267: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			//IL_042b: 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_02f4: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				PlayerControllerB[] array = (((Object)(object)StartOfRound.Instance != (Object)null) ? StartOfRound.Instance.allPlayerScripts : null);
				if (array == null)
				{
					return;
				}
				PlayerControllerB[] array2 = array.Where((PlayerControllerB p) => (Object)(object)p != (Object)null && (Object)(object)p != (Object)(object)local && p.isPlayerControlled && !p.isPlayerDead).ToArray();
				if (array2.Length == 0)
				{
					array2 = (PlayerControllerB[])(object)new PlayerControllerB[1] { local };
				}
				PlayerControllerB val = array2[Random.Range(0, array2.Length)];
				float num = (wander ? Plugin.MoveMinDist.Value : Plugin.StareMinDist.Value);
				float num2 = (wander ? Plugin.MoveMaxDist.Value : Plugin.StareMaxDist.Value);
				float num3 = (forced ? 10f : Plugin.MaxViewAngle.Value);
				Vector3 val2 = ((Component)local).transform.position + Vector3.up * 1.5f;
				bool flag = false;
				Vector3 val3 = Vector3.zero;
				float num4 = 0f;
				int num5 = 0;
				int num6 = 0;
				int num7 = 0;
				int num8 = 0;
				RaycastHit val6 = default(RaycastHit);
				for (int num9 = 0; num9 < Mathf.Max(1, Plugin.SpawnAttempts.Value); num9++)
				{
					num8++;
					num4 = (forced ? 8f : Random.Range(num, num2));
					float num10 = Random.Range(0f - num3, num3);
					Vector3 val4 = Quaternion.Euler(0f, num10, 0f) * ((Component)local).transform.forward;
					Vector3 val5 = ((Component)local).transform.position + val4 * num4;
					if (!Physics.Raycast(new Vector3(val5.x, ((Component)local).transform.position.y + 1.2f, val5.z), Vector3.down, ref val6, 3f, -1, (QueryTriggerInteraction)1) || (Object)(object)((Component)((RaycastHit)(ref val6)).collider).transform.root == (Object)(object)((Component)local).transform)
					{
						num5++;
						continue;
					}
					if (Mathf.Abs(((RaycastHit)(ref val6)).point.y - ((Component)local).transform.position.y) > 1f)
					{
						num5++;
						continue;
					}
					val5.y = ((RaycastHit)(ref val6)).point.y + 0.03f;
					if (!Figure.BodyFits(val5))
					{
						num7++;
						continue;
					}
					bool flag2 = false;
					RaycastHit[] array3 = Physics.RaycastAll(val2, val5 + Vector3.up * 1f - val2, Vector3.Distance(val2, val5) + 1f, -1, (QueryTriggerInteraction)1);
					for (int num11 = 0; num11 < array3.Length; num11++)
					{
						RaycastHit val7 = array3[num11];
						if (!((Object)(object)((Component)((RaycastHit)(ref val7)).collider).transform.root == (Object)(object)((Component)local).transform) && !((Component)((RaycastHit)(ref val7)).collider).transform.IsChildOf(((Component)local).transform))
						{
							flag2 = true;
							break;
						}
					}
					if (flag2 && !forced)
					{
						num6++;
						continue;
					}
					val3 = val5;
					flag = true;
					break;
				}
				if (!flag)
				{
					Plugin.Log.LogInfo((object)$"No clear spot (tried={num8}, groundFails={num5}, wallFails={num7}, losFails={num6}).");
					return;
				}
				Vector3 val8 = ((Component)local).transform.position - val3;
				val8.y = 0f;
				Quaternion rot = ((((Vector3)(ref val8)).sqrMagnitude > 0.01f) ? Quaternion.LookRotation(val8) : Quaternion.identity);
				SkinnedMeshRenderer body;
				GameObject val9 = BuildPlayerClone(val, val3, rot, out body);
				if (!((Object)(object)val9 == (Object)null))
				{
					Animator[] componentsInChildren = val9.GetComponentsInChildren<Animator>(true);
					for (int num11 = 0; num11 < componentsInChildren.Length; num11++)
					{
						((Behaviour)componentsInChildren[num11]).enabled = false;
					}
					Figure figure = val9.AddComponent<Figure>();
					PlayerControllerB val10 = (((Object)(object)val.thisController != (Object)null && val.thisController.isGrounded) ? val : local);
					figure.Init(refFootH: Figure.LowestBoneY(val10.thisPlayerModel) - ((Component)val10).transform.position.y, viewer: local, leaves: wander, body: body);
					Plugin.Log.LogInfo((object)string.Format("Figure: {0} at {1:F1} m as {2}.", wander ? "stare then walk off" : "stare", num4, val.playerUsername));
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Spawn error: " + ex));
			}
		}
	}
	public class Figure : MonoBehaviour
	{
		private PlayerControllerB _viewer;

		private bool _leaves;

		private float _stareEndAt;

		private float _lookTimer;

		private bool _leaving;

		private float _hiddenFor;

		private float _leaveDeadline;

		private Vector3[] _corners;

		private int _corner;

		private SkinnedMeshRenderer _body;

		private float _refFootH;

		private Transform _thighL;

		private Transform _thighR;

		private Transform _shinL;

		private Transform _shinR;

		private Transform _armL;

		private Transform _armR;

		private Quaternion _thighLBase;

		private Quaternion _thighRBase;

		private Quaternion _shinLBase;

		private Quaternion _shinRBase;

		private Quaternion _armLBase;

		private Quaternion _armRBase;

		private Vector3 _thighLAxis;

		private Vector3 _thighRAxis;

		private Vector3 _shinLAxis;

		private Vector3 _shinRAxis;

		private Vector3 _armLAxis;

		private Vector3 _armRAxis;

		private float _gaitPhase;

		internal static bool BodyFits(Vector3 feet)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_0015: 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_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			return !Physics.CheckCapsule(feet + Vector3.up * 0.45f, feet + Vector3.up * 1.55f, 0.3f, StartOfRound.Instance.collidersAndRoomMask, (QueryTriggerInteraction)1);
		}

		internal static float LowestBoneY(SkinnedMeshRenderer smr)
		{
			//IL_0067: 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)
			float num = float.MaxValue;
			if ((Object)(object)smr != (Object)null && smr.bones != null)
			{
				Transform[] bones = smr.bones;
				foreach (Transform val in bones)
				{
					if (!((Object)(object)val == (Object)null))
					{
						string text = ((Object)val).name.ToLowerInvariant();
						if ((text.Contains("foot") || text.Contains("toe") || text.Contains("heel")) && val.position.y < num)
						{
							num = val.position.y;
						}
					}
				}
			}
			if (num != float.MaxValue)
			{
				return num;
			}
			return 0f;
		}

		internal void Init(PlayerControllerB viewer, bool leaves, SkinnedMeshRenderer body, float refFootH)
		{
			_viewer = viewer;
			_leaves = leaves;
			_body = body;
			_refFootH = refFootH;
			_stareEndAt = Time.time + Plugin.StareDuration.Value;
		}

		private static Transform FindBone(SkinnedMeshRenderer smr, string token)
		{
			if ((Object)(object)smr == (Object)null || smr.bones == null)
			{
				return null;
			}
			Transform[] bones = smr.bones;
			foreach (Transform val in bones)
			{
				if ((Object)(object)val != (Object)null && ((Object)val).name.ToLowerInvariant().Contains(token))
				{
					return val;
				}
			}
			return null;
		}

		private void Bind(Transform b, ref Quaternion baseRot, ref Vector3 axis)
		{
			//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_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: 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_004a: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)b == (Object)null))
			{
				baseRot = b.localRotation;
				Vector3 val = (((Object)(object)b.parent != (Object)null) ? b.parent.InverseTransformDirection(((Component)this).transform.right) : Vector3.right);
				axis = ((Vector3)(ref val)).normalized;
			}
		}

		private void StartGait()
		{
			_thighL = FindBone(_body, "thigh.l") ?? FindBone(_body, "thigh_l") ?? FindBone(_body, "upleg.l");
			_thighR = FindBone(_body, "thigh.r") ?? FindBone(_body, "thigh_r") ?? FindBone(_body, "upleg.r");
			_shinL = FindBone(_body, "shin.l") ?? FindBone(_body, "shin_l") ?? FindBone(_body, "calf.l") ?? FindBone(_body, "leg.l");
			_shinR = FindBone(_body, "shin.r") ?? FindBone(_body, "shin_r") ?? FindBone(_body, "calf.r") ?? FindBone(_body, "leg.r");
			_armL = FindBone(_body, "upper_arm.l") ?? FindBone(_body, "upperarm.l");
			_armR = FindBone(_body, "upper_arm.r") ?? FindBone(_body, "upperarm.r");
			Bind(_thighL, ref _thighLBase, ref _thighLAxis);
			Bind(_thighR, ref _thighRBase, ref _thighRAxis);
			Bind(_shinL, ref _shinLBase, ref _shinLAxis);
			Bind(_shinR, ref _shinRBase, ref _shinRAxis);
			Bind(_armL, ref _armLBase, ref _armLAxis);
			Bind(_armR, ref _armRBase, ref _armRAxis);
			_gaitPhase = 0f;
			if ((Object)(object)_thighL == (Object)null && (Object)(object)_thighR == (Object)null)
			{
				Plugin.Log.LogWarning((object)"Gait: no thigh bones in the copy, the figure will slide.");
			}
		}

		private void ApplyGait(float dt)
		{
			//IL_0088: 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_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: 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_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: 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_0138: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_thighL == (Object)null) || !((Object)(object)_thighR == (Object)null))
			{
				float num = Mathf.Clamp(Plugin.StrideLength.Value / Mathf.Max(0.2f, Plugin.WanderSpeed.Value), 0.45f, 2.5f);
				_gaitPhase = Mathf.Repeat(_gaitPhase + dt * MathF.PI * 2f / num, MathF.PI * 2f);
				float num2 = Mathf.Sin(_gaitPhase);
				float num3 = 0f - num2;
				Swing(_thighL, _thighLBase, _thighLAxis, -28f * num2);
				Swing(_thighR, _thighRBase, _thighRAxis, -28f * num3);
				Swing(_shinL, _shinLBase, _shinLAxis, 38f * Mathf.Max(0f, num2));
				Swing(_shinR, _shinRBase, _shinRAxis, 38f * Mathf.Max(0f, num3));
				Swing(_armL, _armLBase, _armLAxis, 14f * num3);
				Swing(_armR, _armRBase, _armRAxis, 14f * num2);
			}
		}

		private static void Swing(Transform b, Quaternion baseRot, Vector3 axis, float deg)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//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_0013: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)b == (Object)null))
			{
				b.localRotation = Quaternion.AngleAxis(deg, axis) * baseRot;
			}
		}

		private bool Occluded(Vector3 feet)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: 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_0022: 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_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//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)
			Vector3 position = ((Component)_viewer.gameplayCamera).transform.position;
			int collidersAndRoomMask = StartOfRound.Instance.collidersAndRoomMask;
			if (Physics.Linecast(position, feet + Vector3.up * 1.6f, collidersAndRoomMask, (QueryTriggerInteraction)1))
			{
				return Physics.Linecast(position, feet + Vector3.up * 0.8f, collidersAndRoomMask, (QueryTriggerInteraction)1);
			}
			return false;
		}

		private bool InView(Vector3 feet)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: 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)
			if (!Occluded(feet))
			{
				return Vector3.Angle(((Component)_viewer.gameplayCamera).transform.forward, feet + Vector3.up * 1.2f - ((Component)_viewer.gameplayCamera).transform.position) < 60f;
			}
			return false;
		}

		private void StartLeaving()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: 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_0093: 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_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: 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_00df: Unknown result type (might be due to invalid IL or missing references)
			_leaving = true;
			_leaveDeadline = Time.time + Plugin.MoveDuration.Value;
			NavMeshPath val = new NavMeshPath();
			Vector3[] corners = null;
			float num = 0f;
			NavMeshHit val3 = default(NavMeshHit);
			for (int i = 0; i < 24; i++)
			{
				Vector2 insideUnitCircle = Random.insideUnitCircle;
				Vector2 val2 = ((Vector2)(ref insideUnitCircle)).normalized * Random.Range(6f, 15f);
				if (NavMesh.SamplePosition(((Component)this).transform.position + new Vector3(val2.x, 0f, val2.y), ref val3, 2.5f, -1) && NavMesh.CalculatePath(((Component)this).transform.position, ((NavMeshHit)(ref val3)).position, -1, val) && (int)val.status == 0)
				{
					if (Occluded(((NavMeshHit)(ref val3)).position))
					{
						_corners = val.corners;
						break;
					}
					float num2 = Vector3.Distance(((Component)_viewer).transform.position, ((NavMeshHit)(ref val3)).position);
					if (num2 > num)
					{
						num = num2;
						corners = val.corners;
					}
				}
			}
			if (_corners == null)
			{
				_corners = corners;
			}
			_corner = 1;
			MonoBehaviour[] componentsInChildren = ((Component)this).GetComponentsInChildren<MonoBehaviour>(true);
			foreach (MonoBehaviour val4 in componentsInChildren)
			{
				if ((((object)val4).GetType().Namespace ?? "").StartsWith("UnityEngine.Animations.Rigging"))
				{
					((Behaviour)val4).enabled = false;
				}
			}
			StartGait();
		}

		private void Update()
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: 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_007d: 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_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: 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_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: 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_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: 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)
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: 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_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Unknown result type (might be due to invalid IL or missing references)
			//IL_029b: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: 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)
			if ((Object)(object)_viewer == (Object)null || _viewer.isPlayerDead || (Object)(object)StartOfRound.Instance == (Object)null)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			if (Vector3.Distance(((Component)_viewer).transform.position, ((Component)this).transform.position) > 45f)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			if (!_leaving)
			{
				Vector3 val = ((Component)_viewer).transform.position - ((Component)this).transform.position;
				val.y = 0f;
				if (((Vector3)(ref val)).sqrMagnitude > 0.01f)
				{
					((Component)this).transform.rotation = Quaternion.LookRotation(val);
				}
				bool flag = Vector3.Angle(((Component)_viewer).transform.forward, val) < 18f && !Occluded(((Component)this).transform.position);
				_lookTimer = (flag ? (_lookTimer + Time.deltaTime) : 0f);
				if (!(_lookTimer > Plugin.StareBreakTime.Value) && !(Time.time > _stareEndAt))
				{
					return;
				}
				if (!_leaves)
				{
					Object.Destroy((Object)(object)((Component)this).gameObject);
					return;
				}
				StartLeaving();
			}
			_hiddenFor = (InView(((Component)this).transform.position) ? 0f : (_hiddenFor + Time.deltaTime));
			if (_hiddenFor > Plugin.VanishDelay.Value)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			ApplyGait(Time.deltaTime);
			if (_corners == null || _corner >= _corners.Length || Time.time > _leaveDeadline)
			{
				return;
			}
			Vector3 val2 = _corners[_corner] - ((Component)this).transform.position;
			val2.y = 0f;
			if (((Vector3)(ref val2)).sqrMagnitude < 0.04f)
			{
				_corner++;
				return;
			}
			Vector3 val3 = ((Component)this).transform.position + ((Vector3)(ref val2)).normalized * Mathf.Min(Plugin.WanderSpeed.Value * Time.deltaTime, ((Vector3)(ref val2)).magnitude);
			RaycastHit val4 = default(RaycastHit);
			if (Physics.Raycast(val3 + Vector3.up * 1.2f, Vector3.down, ref val4, 3f, StartOfRound.Instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1))
			{
				val3.y = ((RaycastHit)(ref val4)).point.y;
			}
			((Component)this).transform.position = val3;
			((Component)this).transform.rotation = Quaternion.Slerp(((Component)this).transform.rotation, Quaternion.LookRotation(val2), 8f * Time.deltaTime);
		}

		private void LateUpdate()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_body == (Object)null) && ((Component)this).transform.childCount != 0)
			{
				Transform child = ((Component)this).transform.GetChild(0);
				child.position += Vector3.up * (_refFootH - (LowestBoneY(_body) - ((Component)this).transform.position.y));
			}
		}
	}
}