Decompiled source of NightmareFredbear v0.5.0

NightmareFredbear.dll

Decompiled an hour ago
using System;
using System.Collections;
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.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json.Linq;
using Photon.Pun;
using Photon.Realtime;
using REPOLib.Modules;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Networking;
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("NightmareFredbear")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.5.0.0")]
[assembly: AssemblyInformationalVersion("0.5.0")]
[assembly: AssemblyProduct("NightmareFredbear")]
[assembly: AssemblyTitle("NightmareFredbear")]
[assembly: AssemblyVersion("0.5.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 NightmareFredbear
{
	internal sealed class FredClip
	{
		internal sealed class Track
		{
			public string Bone;

			public bool Rotation;

			public float[] Times;

			public Quaternion[] Rot;

			public Vector3[] Pos;
		}

		public string Name;

		public float Length;

		public bool Loop;

		public readonly List<Track> Tracks = new List<Track>();
	}
	internal static class FredAssets
	{
		public static GameObject Template;

		public static readonly Dictionary<string, FredClip> Clips = new Dictionary<string, FredClip>();

		public static readonly Dictionary<string, List<AudioClip>> Sounds = new Dictionary<string, List<AudioClip>>();

		public static readonly string[] SoundKeys = new string[13]
		{
			"step", "stomp", "servo", "hum", "aggro", "laugh", "growl", "bite", "crunch", "slam",
			"scream", "spotted", "run"
		};

		private static JObject _j;

		private static byte[] _bin;

		public static IEnumerator Load()
		{
			try
			{
				LoadModel(Find("NightmareFredbear.glb").FirstOrDefault() ?? throw new FileNotFoundException("NightmareFredbear.glb not found under " + Plugin.Dir));
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Model load failed: " + ex));
			}
			yield return LoadSounds();
		}

		private static void Parse(string path)
		{
			byte[] array = File.ReadAllBytes(path);
			if (BitConverter.ToUInt32(array, 0) != 1179937895)
			{
				throw new Exception("not a GLB");
			}
			int num = BitConverter.ToInt32(array, 12);
			_j = JObject.Parse(Encoding.UTF8.GetString(array, 20, num));
			int num2 = 20 + num;
			_bin = new byte[BitConverter.ToInt32(array, num2)];
			Buffer.BlockCopy(array, num2 + 8, _bin, 0, _bin.Length);
		}

		private static void LoadModel(string path)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_0070: 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_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_01a4: 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_01f3: Unknown result type (might be due to invalid IL or missing references)
			Parse(path);
			JArray val = (JArray)_j["nodes"];
			GameObject val2 = new GameObject("NightmareFredbear");
			val2.SetActive(false);
			Object.DontDestroyOnLoad((Object)(object)val2);
			Transform[] array = (Transform[])(object)new Transform[((JContainer)val).Count];
			for (int i = 0; i < ((JContainer)val).Count; i++)
			{
				array[i] = new GameObject(((string)val[i][(object)"name"]) ?? ("node" + i)).transform;
				array[i].SetParent(val2.transform, false);
			}
			for (int j = 0; j < ((JContainer)val).Count; j++)
			{
				JToken val3 = val[j];
				if (val3[(object)"children"] != null)
				{
					foreach (JToken item in (IEnumerable<JToken>)val3[(object)"children"])
					{
						array[(int)item].SetParent(array[j], false);
					}
				}
				JToken obj = val3[(object)"translation"];
				JArray val4 = (JArray)(object)((obj is JArray) ? obj : null);
				if (val4 != null)
				{
					array[j].localPosition = new Vector3(0f - (float)val4[0], (float)val4[1], (float)val4[2]);
				}
				JToken obj2 = val3[(object)"rotation"];
				JArray val5 = (JArray)(object)((obj2 is JArray) ? obj2 : null);
				if (val5 != null)
				{
					array[j].localRotation = new Quaternion((float)val5[0], 0f - (float)val5[1], 0f - (float)val5[2], (float)val5[3]);
				}
				JToken obj3 = val3[(object)"scale"];
				JArray val6 = (JArray)(object)((obj3 is JArray) ? obj3 : null);
				if (val6 != null)
				{
					array[j].localScale = new Vector3((float)val6[0], (float)val6[1], (float)val6[2]);
				}
			}
			for (int k = 0; k < ((JContainer)val).Count; k++)
			{
				if (val[k][(object)"mesh"] != null)
				{
					BuildSkinnedMesh(val[k], array);
				}
			}
			? val7 = (JArray)_j["animations"];
			if ((int)val7 == 0)
			{
				val7 = new JArray();
			}
			foreach (JToken item2 in (JArray)val7)
			{
				FredClip fredClip = ReadClip(item2, val);
				Clips[fredClip.Name] = fredClip;
			}
			Template = val2;
			_j = null;
			_bin = null;
			Plugin.Log.LogInfo((object)string.Format("Model loaded: {0} clips ({1})", Clips.Count, string.Join(", ", Clips.Keys)));
		}

		private static void BuildSkinnedMesh(JToken node, Transform[] t)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0301: Unknown result type (might be due to invalid IL or missing references)
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_032a: Expected O, but got Unknown
			//IL_0147: 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_039f: 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_03f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fa: Expected O, but got Unknown
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_044d: Unknown result type (might be due to invalid IL or missing references)
			//IL_04ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_04ad: Unknown result type (might be due to invalid IL or missing references)
			JArray val = (JArray)_j["meshes"][(object)(int)node[(object)"mesh"]][(object)"primitives"];
			List<Vector3> list = new List<Vector3>();
			List<Vector3> list2 = new List<Vector3>();
			List<Vector2> list3 = new List<Vector2>();
			List<BoneWeight> list4 = new List<BoneWeight>();
			List<int[]> list5 = new List<int[]>();
			List<Material> list6 = new List<Material>();
			int comps;
			foreach (JToken item2 in val)
			{
				JToken val2 = item2[(object)"attributes"];
				int count = list.Count;
				float[] array = Floats((int)val2[(object)"POSITION"], out comps);
				float[] array2 = Floats((int)val2[(object)"NORMAL"], out comps);
				float[] array3 = ((val2[(object)"TEXCOORD_0"] != null) ? Floats((int)val2[(object)"TEXCOORD_0"], out comps) : null);
				float[] array4 = Floats((int)val2[(object)"JOINTS_0"], out comps);
				float[] array5 = Floats((int)val2[(object)"WEIGHTS_0"], out comps);
				int num = array.Length / 3;
				for (int i = 0; i < num; i++)
				{
					list.Add(new Vector3(0f - array[i * 3], array[i * 3 + 1], array[i * 3 + 2]));
					list2.Add(new Vector3(0f - array2[i * 3], array2[i * 3 + 1], array2[i * 3 + 2]));
					list3.Add((Vector2)((array3 != null) ? new Vector2(array3[i * 2], 1f - array3[i * 2 + 1]) : Vector2.zero));
					BoneWeight item = default(BoneWeight);
					((BoneWeight)(ref item)).boneIndex0 = (int)array4[i * 4];
					((BoneWeight)(ref item)).weight0 = array5[i * 4];
					((BoneWeight)(ref item)).boneIndex1 = (int)array4[i * 4 + 1];
					((BoneWeight)(ref item)).weight1 = array5[i * 4 + 1];
					((BoneWeight)(ref item)).boneIndex2 = (int)array4[i * 4 + 2];
					((BoneWeight)(ref item)).weight2 = array5[i * 4 + 2];
					((BoneWeight)(ref item)).boneIndex3 = (int)array4[i * 4 + 3];
					((BoneWeight)(ref item)).weight3 = array5[i * 4 + 3];
					list4.Add(item);
				}
				float[] array6 = Floats((int)item2[(object)"indices"], out comps);
				int[] array7 = new int[array6.Length];
				for (int j = 0; j < array6.Length; j += 3)
				{
					array7[j] = count + (int)array6[j];
					array7[j + 1] = count + (int)array6[j + 2];
					array7[j + 2] = count + (int)array6[j + 1];
				}
				list5.Add(array7);
				list6.Add(MakeMaterial((item2[(object)"material"] != null) ? ((int)item2[(object)"material"]) : (-1)));
			}
			Mesh val3 = new Mesh
			{
				name = (string)node[(object)"name"] + "Mesh",
				indexFormat = (IndexFormat)1
			};
			val3.SetVertices(list);
			val3.SetNormals(list2);
			val3.SetUVs(0, list3);
			val3.subMeshCount = list5.Count;
			for (int k = 0; k < list5.Count; k++)
			{
				val3.SetTriangles(list5[k], k);
			}
			val3.boneWeights = list4.ToArray();
			GameObject val4 = new GameObject(((string)node[(object)"name"]) ?? "Mesh");
			val4.transform.SetParent(t[0].root, false);
			SkinnedMeshRenderer val5 = val4.AddComponent<SkinnedMeshRenderer>();
			JToken obj = _j["skins"][(object)(int)node[(object)"skin"]];
			JArray val6 = (JArray)obj[(object)"joints"];
			float[] array8 = Floats((int)obj[(object)"inverseBindMatrices"], out comps);
			Transform[] array9 = (Transform[])(object)new Transform[((JContainer)val6).Count];
			Matrix4x4[] array10 = (Matrix4x4[])(object)new Matrix4x4[((JContainer)val6).Count];
			for (int l = 0; l < ((JContainer)val6).Count; l++)
			{
				array9[l] = t[(int)val6[l]];
				Matrix4x4 val7 = default(Matrix4x4);
				for (int m = 0; m < 4; m++)
				{
					for (int n = 0; n < 4; n++)
					{
						((Matrix4x4)(ref val7))[n, m] = array8[l * 16 + m * 4 + n] * ((n == 0 != (m == 0)) ? (-1f) : 1f);
					}
				}
				array10[l] = val7;
			}
			val3.bindposes = array10;
			val3.RecalculateBounds();
			val5.sharedMesh = val3;
			val5.bones = array9;
			val5.rootBone = array9[0];
			((Renderer)val5).sharedMaterials = list6.ToArray();
			val5.updateWhenOffscreen = true;
			((Renderer)val5).shadowCastingMode = (ShadowCastingMode)1;
		}

		private static Material MakeMaterial(int index)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0083: 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)
			Material val = new Material(FindShader())
			{
				name = "FredbearSkin"
			};
			JToken val2 = ((index >= 0) ? _j["materials"][(object)index] : null);
			JToken val3 = ((val2 != null) ? val2[(object)"pbrMetallicRoughness"] : null);
			if (((val3 != null) ? val3[(object)"baseColorTexture"] : null) != null)
			{
				val.mainTexture = (Texture)(object)LoadTexture((int)val3[(object)"baseColorTexture"][(object)"index"]);
			}
			val.color = Color.white;
			val.SetFloat("_Metallic", 0.25f);
			val.SetFloat("_Glossiness", 0.35f);
			if (((val2 != null) ? val2[(object)"emissiveTexture"] : null) != null)
			{
				val.EnableKeyword("_EMISSION");
				val.SetTexture("_EmissionMap", (Texture)(object)LoadTexture((int)val2[(object)"emissiveTexture"][(object)"index"]));
				val.SetColor("_EmissionColor", new Color(2.5f, 0.25f, 0.2f));
			}
			return val;
		}

		private static Shader FindShader()
		{
			Shader val = Shader.Find("Standard");
			if ((Object)(object)val != (Object)null)
			{
				return val;
			}
			Renderer[] array = Object.FindObjectsByType<Renderer>((FindObjectsSortMode)0);
			foreach (Renderer val2 in array)
			{
				if ((Object)(object)val2.sharedMaterial != (Object)null && (Object)(object)val2.sharedMaterial.shader != (Object)null)
				{
					return val2.sharedMaterial.shader;
				}
			}
			return Shader.Find("Sprites/Default");
		}

		private static Texture2D LoadTexture(int texIndex)
		{
			//IL_00af: 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_00bb: Expected O, but got Unknown
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Expected O, but got Unknown
			int num = (int)_j["textures"][(object)texIndex][(object)"source"];
			JToken obj = _j["bufferViews"][(object)(int)_j["images"][(object)num][(object)"bufferView"]];
			int valueOrDefault = ((int?)obj[(object)"byteOffset"]).GetValueOrDefault();
			int num2 = (int)obj[(object)"byteLength"];
			byte[] array = new byte[num2];
			Buffer.BlockCopy(_bin, valueOrDefault, array, 0, num2);
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, true);
			ImageConversion.LoadImage(val, array);
			((Texture)val).wrapMode = (TextureWrapMode)1;
			return val;
		}

		private static float[] Floats(int accessor, out int comps)
		{
			JToken val = _j["accessors"][(object)accessor];
			int num = (int)val[(object)"count"];
			comps = TypeSize((string)val[(object)"type"]);
			int num2 = (int)val[(object)"componentType"];
			int num3;
			switch (num2)
			{
			default:
				num3 = 1;
				break;
			case 5122:
			case 5123:
				num3 = 2;
				break;
			case 5125:
			case 5126:
				num3 = 4;
				break;
			}
			int num4 = num3;
			JToken obj = _j["bufferViews"][(object)(int)val[(object)"bufferView"]];
			int num5 = ((int?)obj[(object)"byteOffset"]).GetValueOrDefault() + ((int?)val[(object)"byteOffset"]).GetValueOrDefault();
			int num6 = ((int?)obj[(object)"byteStride"]) ?? (comps * num4);
			bool valueOrDefault = (bool?)val[(object)"normalized"] == true;
			float[] array = new float[num * comps];
			for (int i = 0; i < num; i++)
			{
				for (int j = 0; j < comps; j++)
				{
					int num7 = num5 + i * num6 + j * num4;
					float num8;
					switch (num2)
					{
					case 5126:
						num8 = BitConverter.ToSingle(_bin, num7);
						break;
					case 5125:
						num8 = BitConverter.ToUInt32(_bin, num7);
						break;
					case 5123:
						num8 = (int)BitConverter.ToUInt16(_bin, num7);
						if (valueOrDefault)
						{
							num8 /= 65535f;
						}
						break;
					case 5122:
						num8 = BitConverter.ToInt16(_bin, num7);
						if (valueOrDefault)
						{
							num8 = Mathf.Max(num8 / 32767f, -1f);
						}
						break;
					case 5121:
						num8 = (int)_bin[num7];
						if (valueOrDefault)
						{
							num8 /= 255f;
						}
						break;
					default:
						num8 = (sbyte)_bin[num7];
						if (valueOrDefault)
						{
							num8 = Mathf.Max(num8 / 127f, -1f);
						}
						break;
					}
					array[i * comps + j] = num8;
				}
			}
			return array;
		}

		private static int TypeSize(string t)
		{
			return t switch
			{
				"VEC4" => 4, 
				"VEC3" => 3, 
				"VEC2" => 2, 
				"SCALAR" => 1, 
				_ => 16, 
			};
		}

		private static FredClip ReadClip(JToken a, JArray nodes)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			//IL_0222: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0247: 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_0251: 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_01c7: Unknown result type (might be due to invalid IL or missing references)
			FredClip fredClip = new FredClip
			{
				Name = (string)a[(object)"name"]
			};
			fredClip.Loop = !fredClip.Name.StartsWith("Kill") && fredClip.Name != "Spotted_Scream";
			JArray val = (JArray)a[(object)"samplers"];
			foreach (JToken item in (IEnumerable<JToken>)a[(object)"channels"])
			{
				string text = (string)item[(object)"target"][(object)"path"];
				if (text != "rotation" && text != "translation")
				{
					continue;
				}
				JToken obj = val[(int)item[(object)"sampler"]];
				int comps;
				float[] array = Floats((int)obj[(object)"input"], out comps);
				float[] array2 = Floats((int)obj[(object)"output"], out comps);
				FredClip.Track track = new FredClip.Track
				{
					Bone = (string)nodes[(int)item[(object)"target"][(object)"node"]][(object)"name"],
					Rotation = (text == "rotation"),
					Times = array
				};
				int num = array.Length;
				bool flag = true;
				if (track.Rotation)
				{
					track.Rot = (Quaternion[])(object)new Quaternion[num];
					for (int i = 0; i < num; i++)
					{
						track.Rot[i] = new Quaternion(array2[i * 4], 0f - array2[i * 4 + 1], 0f - array2[i * 4 + 2], array2[i * 4 + 3]);
						if (i > 0 && Quaternion.Angle(track.Rot[i], track.Rot[0]) > 0.05f)
						{
							flag = false;
						}
					}
				}
				else
				{
					track.Pos = (Vector3[])(object)new Vector3[num];
					for (int j = 0; j < num; j++)
					{
						track.Pos[j] = new Vector3(0f - array2[j * 3], array2[j * 3 + 1], array2[j * 3 + 2]);
						if (j > 0)
						{
							Vector3 val2 = track.Pos[j] - track.Pos[0];
							if (((Vector3)(ref val2)).sqrMagnitude > 1E-06f)
							{
								flag = false;
							}
						}
					}
					if (flag)
					{
						continue;
					}
				}
				fredClip.Tracks.Add(track);
				fredClip.Length = Mathf.Max(fredClip.Length, array[num - 1]);
			}
			return fredClip;
		}

		private static IEnumerator LoadSounds()
		{
			string[] soundKeys = SoundKeys;
			foreach (string text in soundKeys)
			{
				List<AudioClip> list = (Sounds[text] = new List<AudioClip>());
				List<AudioClip> list3 = list;
				string[] array = Find("fb_" + text + "_*.ogg");
				foreach (string f in array)
				{
					UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip("file:///" + f.Replace('\\', '/'), (AudioType)14);
					try
					{
						yield return req.SendWebRequest();
						if ((int)req.result == 1)
						{
							AudioClip content = DownloadHandlerAudioClip.GetContent(req);
							((Object)content).name = Path.GetFileNameWithoutExtension(f);
							list3.Add(content);
						}
						else
						{
							Plugin.Log.LogWarning((object)("Sound " + f + ": " + req.error));
						}
					}
					finally
					{
						((IDisposable)req)?.Dispose();
					}
				}
			}
			Plugin.Log.LogInfo((object)("Sounds: " + string.Join(", ", SoundKeys.Select((string k) => k + "=" + Sounds[k].Count))));
		}

		private static string[] Find(string pattern)
		{
			return Directory.GetFiles(Plugin.Dir, pattern, SearchOption.AllDirectories);
		}

		public static AudioClip Random(string key)
		{
			if (!Sounds.TryGetValue(key, out var value) || value.Count <= 0)
			{
				return null;
			}
			return value[Random.Range(0, value.Count)];
		}
	}
	internal sealed class FredDirector : MonoBehaviour
	{
		public static FredDirector Instance;

		private static readonly FieldRef<PlayerAvatar, bool> IsDisabled = AccessTools.FieldRefAccess<PlayerAvatar, bool>("isDisabled");

		private static readonly FieldRef<PlayerAvatar, bool> DeadSet = AccessTools.FieldRefAccess<PlayerAvatar, bool>("deadSet");

		private static readonly FieldRef<PlayerAvatar, bool> IsSprinting = AccessTools.FieldRefAccess<PlayerAvatar, bool>("isSprinting");

		private static readonly FieldRef<PlayerAvatar, bool> IsCrouching = AccessTools.FieldRefAccess<PlayerAvatar, bool>("isCrouching");

		private static readonly FieldRef<PlayerAvatar, bool> IsCrawling = AccessTools.FieldRefAccess<PlayerAvatar, bool>("isCrawling");

		private static readonly FieldRef<PlayerAvatar, PlayerVoiceChat> Voice = AccessTools.FieldRefAccess<PlayerAvatar, PlayerVoiceChat>("voiceChat");

		private static readonly FieldRef<PlayerVoiceChat, float> Loudness = AccessTools.FieldRefAccess<PlayerVoiceChat, float>("clipLoudness");

		private static readonly FieldRef<LevelPoint, bool> InStartRoom = AccessTools.FieldRefAccess<LevelPoint, bool>("inStartRoom");

		private FredEntity _e;

		private NavMeshAgent _agent;

		private LevelGenerator _level;

		private readonly List<Vector3> _points = new List<Vector3>();

		private readonly Dictionary<int, float> _meter = new Dictionary<int, float>();

		private bool _rolled;

		private bool _active;

		private bool _hasGoal;

		private float _levelTime;

		private float _stateTime;

		private float _sendTimer;

		private float _moveTimer;

		private float _noSight;

		private float _grace;

		private float _stuckTimer;

		private PlayerAvatar _target;

		private Vector3 _goal;

		private Vector3 _lastSeen;

		private Vector3 _stuckPos;

		private int _netTarget = -1;

		private int _hp;

		private float _respawn;

		private float _stagger;

		private readonly Dictionary<int, float> _hitCd = new Dictionary<int, float>();

		private static readonly FieldRef<EnemyParent, bool> EpSpawned = AccessTools.FieldRefAccess<EnemyParent, bool>("Spawned");

		private static readonly FieldRef<EnemyParent, Enemy> EpEnemy = AccessTools.FieldRefAccess<EnemyParent, Enemy>("Enemy");

		private static readonly FieldRef<Enemy, EnemyParent> EParent = AccessTools.FieldRefAccess<Enemy, EnemyParent>("EnemyParent");

		private static readonly FieldRef<Enemy, EnemyHealth> EHealth = AccessTools.FieldRefAccess<Enemy, EnemyHealth>("Health");

		private static readonly FieldRef<Enemy, bool> EHasHealth = AccessTools.FieldRefAccess<Enemy, bool>("HasHealth");

		private static readonly FieldRef<EnemyHealth, bool> EDead = AccessTools.FieldRefAccess<EnemyHealth, bool>("dead");

		private Enemy _prey;

		private Enemy _killPrey;

		private float _killImpact;

		private bool _jumping;

		private bool _offMesh;

		private float _jT;

		private float _jDur;

		private float _jH;

		private float _offTimer;

		private float _jumpCd;

		private float _stuck;

		private float _lastFlat = float.MaxValue;

		private Vector3 _jFrom;

		private Vector3 _jTo;

		private static int VisionMask => LayerMask.op_Implicit(SemiFunc.LayerMaskGetVisionObstruct()) | LayerMask.GetMask(new string[1] { "HideTriggers" });

		private static bool InLevel
		{
			get
			{
				if (SemiFunc.RunIsLevel() && (Object)(object)LevelGenerator.Instance != (Object)null)
				{
					return LevelGenerator.Instance.Generated;
				}
				return false;
			}
		}

		private bool PreyValid => Alive(_prey);

		private Vector3 PreyCenter => Center(_prey);

		private bool HasTarget
		{
			get
			{
				if (!((Object)(object)_target != (Object)null))
				{
					return PreyValid;
				}
				return Valid(_target);
			}
		}

		private Vector3 TargetPos
		{
			get
			{
				//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)
				//IL_0026: Unknown result type (might be due to invalid IL or missing references)
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_0033: 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_0049: 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)
				if ((Object)(object)_target != (Object)null)
				{
					return ((Component)_target).transform.position;
				}
				Vector3 preyCenter = PreyCenter;
				RaycastHit val = default(RaycastHit);
				if (!Physics.Raycast(preyCenter, Vector3.down, ref val, 3f, LayerMask.op_Implicit(SemiFunc.LayerMaskGetVisionObstruct()), (QueryTriggerInteraction)1))
				{
					return preyCenter;
				}
				return ((RaycastHit)(ref val)).point;
			}
		}

		private void Awake()
		{
			Instance = this;
		}

		private void Update()
		{
			if ((Object)(object)FredAssets.Template == (Object)null)
			{
				return;
			}
			LevelGenerator instance = LevelGenerator.Instance;
			if ((Object)(object)instance == (Object)null || !InLevel)
			{
				if ((Object)(object)_e != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)_e).gameObject);
				}
				_e = null;
				_level = null;
				return;
			}
			if ((Object)(object)instance != (Object)(object)_level)
			{
				ResetLevel(instance);
			}
			if ((Object)(object)_e != (Object)null)
			{
				_e.LookAt = LookPoint();
			}
			if (FredNet.IsHost)
			{
				HostTick(Time.deltaTime);
			}
		}

		private void ResetLevel(LevelGenerator lg)
		{
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			_level = lg;
			if ((Object)(object)_e != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)_e).gameObject);
			}
			_e = null;
			_agent = null;
			_target = null;
			_prey = (_killPrey = null);
			_jumping = (_offMesh = false);
			_rolled = (_active = false);
			_levelTime = 0f;
			_meter.Clear();
			_hitCd.Clear();
			_points.Clear();
			foreach (LevelPoint item in SemiFunc.LevelPointsGetAll())
			{
				if ((Object)(object)item != (Object)null && !item.Truck && !InStartRoom.Invoke(item))
				{
					_points.Add(((Component)item).transform.position);
				}
			}
		}

		public static List<PlayerAvatar> Alive()
		{
			List<PlayerAvatar> list = new List<PlayerAvatar>();
			foreach (PlayerAvatar item in SemiFunc.PlayerGetList())
			{
				if ((Object)(object)item != (Object)null && !IsDisabled.Invoke(item) && !DeadSet.Invoke(item))
				{
					list.Add(item);
				}
			}
			return list;
		}

		private static Vector3 Head(PlayerAvatar p)
		{
			//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)
			//IL_002f: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)p == (Object)(object)SemiFunc.PlayerAvatarLocal()) || !((Object)(object)SemiFunc.MainCamera() != (Object)null))
			{
				return ((Component)p).transform.position + Vector3.up * 1.5f;
			}
			return ((Component)SemiFunc.MainCamera()).transform.position;
		}

		private static int ViewId(PlayerAvatar p)
		{
			if (!((Object)(object)p != (Object)null) || !((Object)(object)p.photonView != (Object)null))
			{
				return -1;
			}
			return p.photonView.ViewID;
		}

		private Vector3? LookPoint()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_e == (Object)null || (_e.State != FredState.Aggro && _e.State != FredState.Chase))
			{
				return null;
			}
			if (FredNet.IsHost && (Object)(object)_target == (Object)null && PreyValid)
			{
				return PreyCenter;
			}
			PlayerAvatar val = (FredNet.IsHost ? _target : ((_netTarget >= 0) ? SemiFunc.PlayerAvatarGetFromPhotonID(_netTarget) : null));
			if (!((Object)(object)val != (Object)null))
			{
				return null;
			}
			return Head(val);
		}

		public void ClientState(FredState s, Vector3 pos, Quaternion rot, int target)
		{
			//IL_004e: 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_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)FredAssets.Template == (Object)null || !InLevel)
			{
				return;
			}
			_netTarget = target;
			if ((Object)(object)_e == (Object)null)
			{
				if (s == FredState.Hidden)
				{
					return;
				}
				_e = FredEntity.Create(pos, rot);
			}
			_e.Remote = true;
			_e.NetPos = pos;
			_e.NetRot = rot;
			if (s != FredState.Kill)
			{
				_e.SetState(s);
			}
		}

		public void ForceSpawn()
		{
			if (FredNet.IsHost && !((Object)(object)_level == (Object)null))
			{
				_rolled = (_active = true);
				Spawn(near: true);
			}
		}

		private void HostTick(float dt)
		{
			_levelTime += dt;
			if (!_rolled && _levelTime > Plugin.SpawnDelay.Value)
			{
				_rolled = true;
				_active = Random.value < Plugin.SpawnChance.Value;
				if (_active)
				{
					Spawn(near: false);
				}
			}
			if (!((Object)(object)_e == (Object)null))
			{
				Think(dt);
				_sendTimer -= dt;
				if (_sendTimer <= 0f)
				{
					Broadcast();
				}
			}
		}

		private void Broadcast()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			_sendTimer = 0.1f;
			FredNet.SendState(_e.State, ((Component)_e).transform.position, ((Component)_e).transform.rotation, ViewId(_target));
		}

		private void Enter(FredState s)
		{
			Plugin.Log.LogDebug((object)$"Fredbear {_e.State} -> {s}");
			_stateTime = 0f;
			_hasGoal = false;
			_e.SetState(s);
			Broadcast();
		}

		private void Spawn(bool near)
		{
			//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_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: 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_01d6: 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_01dc: 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_01e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: 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_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0303: Unknown result type (might be due to invalid IL or missing references)
			//IL_0361: 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_012a: 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_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: 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_00c4: 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)
			List<PlayerAvatar> list = Alive();
			if (list.Count == 0 || _points.Count == 0)
			{
				return;
			}
			Vector3 val = Vector3.zero;
			bool flag = false;
			if (near)
			{
				NavMeshHit val3 = default(NavMeshHit);
				for (int i = 0; i < 40; i++)
				{
					if (flag)
					{
						break;
					}
					Vector3 val2 = _points[Random.Range(0, _points.Count)];
					float num = float.MaxValue;
					foreach (PlayerAvatar item in list)
					{
						num = Mathf.Min(num, Vector3.Distance(val2, ((Component)item).transform.position));
					}
					if (!(num < 8f) && !(num > 22f) && NavMesh.SamplePosition(val2, ref val3, 3f, -1))
					{
						val = ((NavMeshHit)(ref val3)).position;
						flag = true;
					}
				}
			}
			else
			{
				float num2 = -1f;
				NavMeshHit val4 = default(NavMeshHit);
				foreach (Vector3 point in _points)
				{
					float num3 = float.MaxValue;
					foreach (PlayerAvatar item2 in list)
					{
						num3 = Mathf.Min(num3, Vector3.Distance(point, ((Component)item2).transform.position));
					}
					if (num3 > num2 && NavMesh.SamplePosition(point, ref val4, 3f, -1))
					{
						num2 = num3;
						val = ((NavMeshHit)(ref val4)).position;
						flag = true;
					}
				}
			}
			if (!flag)
			{
				Plugin.Log.LogWarning((object)"No spawn point found, retry later");
				_rolled = false;
				_levelTime = Plugin.SpawnDelay.Value - 5f;
				return;
			}
			Vector3 val5 = Flat(((Component)list[0]).transform.position - val);
			Quaternion rot = Quaternion.LookRotation(((Vector3)(ref val5)).normalized + Vector3.forward * 0.001f);
			if ((Object)(object)_e != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)_e).gameObject);
			}
			NavMeshAgent val6 = Object.FindAnyObjectByType<NavMeshAgent>();
			_e = FredEntity.Create(val, rot);
			((Component)_e).gameObject.SetActive(false);
			_agent = ((Component)_e).gameObject.AddComponent<NavMeshAgent>();
			if ((Object)(object)val6 != (Object)null)
			{
				_agent.agentTypeID = val6.agentTypeID;
			}
			_agent.radius = 0.4f / Plugin.Scale.Value;
			_agent.height = 2.2f;
			_agent.stoppingDistance = 0.3f;
			_agent.obstacleAvoidanceType = (ObstacleAvoidanceType)0;
			_agent.updateRotation = false;
			_agent.acceleration = 25f;
			_agent.angularSpeed = 360f;
			((Component)_e).gameObject.SetActive(true);
			_agent.Warp(val);
			_meter.Clear();
			_target = null;
			_prey = (_killPrey = null);
			_jumping = (_offMesh = false);
			_hp = Plugin.Health.Value;
			Enter(FredState.Patrol);
			Plugin.Log.LogInfo((object)$"Nightmare Fredbear appeared at {val}");
		}

		private void Think(float dt)
		{
			//IL_020f: 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_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0273: 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_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: 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_02dc: 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_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0315: Unknown result type (might be due to invalid IL or missing references)
			//IL_031a: Unknown result type (might be due to invalid IL or missing references)
			//IL_031c: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a5: 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_0351: Unknown result type (might be due to invalid IL or missing references)
			//IL_0385: Unknown result type (might be due to invalid IL or missing references)
			//IL_038a: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_03da: Unknown result type (might be due to invalid IL or missing references)
			//IL_03df: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b1: 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_0406: Unknown result type (might be due to invalid IL or missing references)
			//IL_040b: Unknown result type (might be due to invalid IL or missing references)
			if (_jumping)
			{
				JumpStep(dt);
				return;
			}
			if (_offMesh && _e.State != FredState.Chase && _e.State != FredState.Kill)
			{
				JumpDown(((Component)_e).transform.position);
			}
			_stateTime += dt;
			_grace -= dt;
			if (_stagger > 0f && _e.State != FredState.Kill && _e.State != FredState.Hidden)
			{
				_stagger -= dt;
				Stop();
				return;
			}
			Vector3 val;
			switch (_e.State)
			{
			case FredState.Hidden:
				_respawn -= dt;
				if (_respawn <= 0f)
				{
					Spawn(near: false);
				}
				break;
			case FredState.Patrol:
				if (!_hasGoal)
				{
					_goal = PatrolGoal();
					_hasGoal = true;
					_stuckTimer = 0f;
					_stuckPos = ((Component)_e).transform.position;
				}
				MoveTo(_goal, Plugin.WalkSpeed.Value);
				_stuckTimer += dt;
				if (_stuckTimer > 4f)
				{
					val = ((Component)_e).transform.position - _stuckPos;
					if (((Vector3)(ref val)).sqrMagnitude < 0.25f)
					{
						_hasGoal = false;
					}
					_stuckTimer = 0f;
					_stuckPos = ((Component)_e).transform.position;
				}
				if (_hasGoal && Arrived())
				{
					if (Random.value < 0.75f)
					{
						Enter(FredState.Look);
					}
					else
					{
						_hasGoal = false;
					}
				}
				Sense(dt);
				break;
			case FredState.Look:
				Stop();
				if (_stateTime > 5f)
				{
					Enter(FredState.Patrol);
				}
				Sense(dt);
				break;
			case FredState.Investigate:
				MoveTo(_goal, Plugin.WalkSpeed.Value * 1.5f);
				if (Arrived() || _stateTime > 20f)
				{
					Enter(FredState.Look);
				}
				Sense(dt);
				break;
			case FredState.Aggro:
				Stop();
				if (!HasTarget)
				{
					_target = null;
					_prey = null;
					Enter(FredState.Patrol);
					break;
				}
				Face(TargetPos, 1f - Mathf.Exp(-12f * dt));
				if (_stateTime > 1.2f)
				{
					_noSight = 0f;
					Enter(FredState.Chase);
				}
				break;
			case FredState.Chase:
			{
				if (!HasTarget)
				{
					_target = null;
					_prey = null;
					if (_offMesh)
					{
						JumpDown(((Component)_e).transform.position);
					}
					Enter(FredState.Investigate);
					break;
				}
				if ((Object)(object)_prey != (Object)null)
				{
					Sense(dt);
				}
				if (_e.State != FredState.Chase)
				{
					break;
				}
				Vector3 targetPos = TargetPos;
				ChaseMove(targetPos, dt);
				if (((Object)(object)_target != (Object)null) ? Sees(_target, out var _) : SeesMonster(_prey))
				{
					_lastSeen = targetPos;
					_noSight = 0f;
				}
				else
				{
					_noSight += dt;
				}
				if (_noSight > Plugin.LoseSight.Value)
				{
					_goal = _lastSeen;
					_target = null;
					_prey = null;
					if (_offMesh)
					{
						JumpDown(((Component)_e).transform.position);
					}
					Enter(FredState.Investigate);
				}
				else
				{
					if (_jumping)
					{
						break;
					}
					Vector3 val2 = targetPos - ((Component)_e).transform.position;
					if (!(Mathf.Abs(val2.y) < 1.6f * Plugin.Scale.Value))
					{
						break;
					}
					val = Flat(val2);
					if (((Vector3)(ref val)).magnitude < Plugin.KillRange.Value * Plugin.Scale.Value)
					{
						if ((Object)(object)_target != (Object)null)
						{
							Kill(_target);
						}
						else
						{
							KillMonster(_prey);
						}
					}
				}
				break;
			}
			case FredState.Kill:
				Stop();
				if ((Object)(object)_killPrey != (Object)null && _stateTime >= _killImpact)
				{
					if (Alive(_killPrey))
					{
						EHealth.Invoke(_killPrey).Hurt(99999, ((Component)_e).transform.forward);
					}
					_killPrey = null;
				}
				if (_stateTime > FredAssets.Clips[FredEntity.KillClips[_e.KillIndex]].Length + 0.4f)
				{
					_target = null;
					_prey = null;
					_meter.Clear();
					_grace = 5f;
					Enter(FredState.Patrol);
				}
				break;
			}
		}

		private Vector3 PatrolGoal()
		{
			//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_0021: 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_0080: 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_005a: Unknown result type (might be due to invalid IL or missing references)
			NavMeshHit val2 = default(NavMeshHit);
			for (int i = 0; i < 20; i++)
			{
				Vector3 val = _points[Random.Range(0, _points.Count)];
				float num = Vector3.Distance(val, ((Component)_e).transform.position);
				if (num > 6f && num < 45f && NavMesh.SamplePosition(val, ref val2, 3f, -1))
				{
					return ((NavMeshHit)(ref val2)).position;
				}
			}
			return _points[Random.Range(0, _points.Count)];
		}

		private bool Sees(PlayerAvatar p, out float rate)
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: 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_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: 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_0115: 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_011c: Unknown result type (might be due to invalid IL or missing references)
			rate = 0f;
			bool flag = IsCrawling.Invoke(p);
			bool flag2 = IsCrouching.Invoke(p);
			float num = Plugin.VisionRange.Value * (flag ? 0.35f : (flag2 ? 0.6f : 1f));
			float num2 = (flag ? 1f : (flag2 ? 1.6f : 2.6f));
			Vector3 eyePosition = _e.EyePosition;
			Vector3 val = (flag ? (((Component)p).transform.position + Vector3.up * 0.3f) : (flag2 ? (((Component)p).transform.position + Vector3.up * 0.8f) : Head(p))) - eyePosition;
			float magnitude = ((Vector3)(ref val)).magnitude;
			if (magnitude > num)
			{
				return false;
			}
			float num3 = ((_e.State == FredState.Look) ? 85f : ((_e.State == FredState.Chase) ? 120f : 60f));
			if (magnitude > num2 && Vector3.Angle(((Component)_e).transform.forward, Flat(val)) > num3)
			{
				return false;
			}
			RaycastHit val2 = default(RaycastHit);
			if (Physics.Raycast(eyePosition, val / magnitude, ref val2, magnitude, VisionMask, (QueryTriggerInteraction)2) && (Object)(object)((Component)((RaycastHit)(ref val2)).transform).GetComponentInParent<PlayerAvatar>() != (Object)(object)p)
			{
				return false;
			}
			rate = ((magnitude <= num2) ? 99f : (flag ? 0.2f : (flag2 ? 0.4f : 2f)));
			return true;
		}

		private void Sense(float dt)
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: 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_01f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
			if (_grace > 0f)
			{
				return;
			}
			PlayerAvatar val = null;
			float num = 0f;
			foreach (PlayerAvatar item in Alive())
			{
				int key = ViewId(item);
				_meter.TryGetValue(key, out var value);
				float num2 = Vector3.Distance(((Component)item).transform.position, ((Component)_e).transform.position);
				value = ((!Sees(item, out var rate)) ? Mathf.Max(0f, value - 0.5f * dt) : (value + rate * dt));
				if (IsSprinting.Invoke(item) && num2 < 7f)
				{
					value = 1f;
				}
				PlayerVoiceChat val2 = Voice.Invoke(item);
				if ((Object)(object)val2 != (Object)null && Loudness.Invoke(val2) > 0.2f && num2 < 6f)
				{
					value = 1f;
				}
				_meter[key] = value;
				if (value > num)
				{
					num = value;
					val = item;
				}
			}
			if ((Object)(object)val != (Object)null && num >= 1f)
			{
				_target = val;
				_prey = null;
				_lastSeen = ((Component)val).transform.position;
				Enter(FredState.Aggro);
			}
			else
			{
				if (_e.State == FredState.Chase)
				{
					return;
				}
				Enemy val3 = null;
				float num3 = float.MaxValue;
				foreach (Enemy item2 in Monsters())
				{
					if (SeesMonster(item2))
					{
						float num4 = Vector3.Distance(Center(item2), ((Component)_e).transform.position);
						if (num4 < num3)
						{
							num3 = num4;
							val3 = item2;
						}
					}
				}
				if ((Object)(object)val3 != (Object)null)
				{
					_target = null;
					_prey = val3;
					_lastSeen = Center(val3);
					Enter(FredState.Aggro);
				}
			}
		}

		public void Hear(Vector3 pos, float radius)
		{
			//IL_004e: 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_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: 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_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			if (!FredNet.IsHost || (Object)(object)_e == (Object)null || _grace > 0f || (_e.State != FredState.Patrol && _e.State != FredState.Look && _e.State != FredState.Investigate))
			{
				return;
			}
			float num = Vector3.Distance(pos, ((Component)_e).transform.position);
			if (num > Mathf.Max(radius, 4f) * 1.5f)
			{
				return;
			}
			if (radius >= 8f && num < radius)
			{
				PlayerAvatar val = null;
				float num2 = 4f;
				foreach (PlayerAvatar item in Alive())
				{
					float num3 = Vector3.Distance(((Component)item).transform.position, pos);
					if (num3 < num2)
					{
						num2 = num3;
						val = item;
					}
				}
				if ((Object)(object)val != (Object)null)
				{
					_target = val;
					_prey = null;
					_lastSeen = ((Component)val).transform.position;
					Enter(FredState.Aggro);
					return;
				}
			}
			NavMeshHit val2 = default(NavMeshHit);
			_goal = (NavMesh.SamplePosition(pos, ref val2, 3f, -1) ? ((NavMeshHit)(ref val2)).position : pos);
			Enter(FredState.Investigate);
			_hasGoal = true;
		}

		private static bool Alive(Enemy m)
		{
			if ((Object)(object)m == (Object)null || !((Component)m).gameObject.activeInHierarchy || !EHasHealth.Invoke(m))
			{
				return false;
			}
			EnemyParent val = EParent.Invoke(m);
			EnemyHealth val2 = EHealth.Invoke(m);
			if ((Object)(object)val != (Object)null && EpSpawned.Invoke(val) && (Object)(object)val2 != (Object)null)
			{
				return !EDead.Invoke(val2);
			}
			return false;
		}

		private static List<Enemy> Monsters()
		{
			List<Enemy> list = new List<Enemy>();
			EnemyDirector instance = EnemyDirector.instance;
			if ((Object)(object)instance == (Object)null || instance.enemiesSpawned == null)
			{
				return list;
			}
			foreach (EnemyParent item in instance.enemiesSpawned)
			{
				if ((Object)(object)item != (Object)null && EpSpawned.Invoke(item) && Alive(EpEnemy.Invoke(item)))
				{
					list.Add(EpEnemy.Invoke(item));
				}
			}
			return list;
		}

		private static Vector3 Center(Enemy m)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)m.CenterTransform != (Object)null))
			{
				return ((Component)m).transform.position;
			}
			return m.CenterTransform.position;
		}

		private bool SeesMonster(Enemy m)
		{
			//IL_0006: 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_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)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: 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_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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)
			Vector3 eyePosition = _e.EyePosition;
			Vector3 val = Center(m) - eyePosition;
			float magnitude = ((Vector3)(ref val)).magnitude;
			if (magnitude > Plugin.VisionRange.Value * Plugin.Scale.Value)
			{
				return false;
			}
			if (magnitude > 3f && Vector3.Angle(((Component)_e).transform.forward, Flat(val)) > ((_e.State == FredState.Look) ? 85f : 70f))
			{
				return false;
			}
			RaycastHit val2 = default(RaycastHit);
			if (Physics.Raycast(eyePosition, val / magnitude, ref val2, magnitude, VisionMask, (QueryTriggerInteraction)1))
			{
				return (Object)(object)((Component)((RaycastHit)(ref val2)).transform).GetComponentInParent<EnemyParent>() == (Object)(object)EParent.Invoke(m);
			}
			return true;
		}

		private void KillMonster(Enemy m)
		{
			//IL_000f: 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_0024: 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_0031: 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_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: 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_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			Stop();
			int num = Random.Range(0, 2);
			Vector3 val = Flat(Center(m) - ((Component)_e).transform.position);
			Quaternion val2 = Quaternion.LookRotation(((Vector3)(ref val)).normalized + ((Component)_e).transform.forward * 0.001f);
			((Component)_e).transform.rotation = val2;
			_stateTime = 0f;
			_killPrey = m;
			_killImpact = FredEntity.ImpactTime[num];
			FredNet.SendKill(-1, num, ((Component)_e).transform.position, val2);
			PlayKill(-1, num, ((Component)_e).transform.position, val2);
			Broadcast();
		}

		private void StartJump(Vector3 to, float height)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: 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_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)_agent != (Object)null)
			{
				((Behaviour)_agent).enabled = false;
			}
			_jFrom = ((Component)_e).transform.position;
			_jTo = to;
			_jH = height;
			_jDur = 0.45f + Vector3.Distance(_jFrom, to) * 0.06f;
			_jT = 0f;
			_jumping = true;
			_jumpCd = 2f;
			_e.PlayKey("growl", 0.8f);
		}

		private void JumpStep(float dt)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			_jT += dt / _jDur;
			float num = Mathf.Clamp01(_jT);
			((Component)_e).transform.position = Vector3.Lerp(_jFrom, _jTo, num) + Vector3.up * (4f * _jH * num * (1f - num));
			if (!(num < 1f))
			{
				_jumping = false;
				_e.PlayKey("slam", 0.5f);
				NavMeshHit val = default(NavMeshHit);
				if (NavMesh.SamplePosition(((Component)_e).transform.position, ref val, 0.6f * Plugin.Scale.Value, -1))
				{
					_offMesh = false;
					((Behaviour)_agent).enabled = true;
					_agent.Warp(((NavMeshHit)(ref val)).position);
				}
				else
				{
					_offMesh = true;
					_offTimer = 0f;
				}
			}
		}

		private void JumpDown(Vector3 near)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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)
			NavMeshHit val = default(NavMeshHit);
			if (NavMesh.SamplePosition(near, ref val, 4f, -1) || NavMesh.SamplePosition(((Component)_e).transform.position, ref val, 8f, -1))
			{
				_offMesh = false;
				StartJump(((NavMeshHit)(ref val)).position, 0.6f);
			}
		}

		private void ChaseMove(Vector3 tp, float dt)
		{
			//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_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: 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_0137: 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)
			//IL_013d: 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_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: 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_003c: 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_0043: 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_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: 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_007b: 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_0098: 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_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_00b0: 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_00e8: 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_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_0207: Unknown result type (might be due to invalid IL or missing references)
			//IL_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			float value = Plugin.Scale.Value;
			Vector3 position = ((Component)_e).transform.position;
			if (_offMesh)
			{
				_offTimer += dt;
				Vector3 val = Flat(tp - position);
				Vector3 val2 = position + ((Vector3)(ref val)).normalized * Mathf.Min(((Vector3)(ref val)).magnitude, Plugin.SprintSpeed.Value * 0.6f * dt);
				RaycastHit val3 = default(RaycastHit);
				if (tp.y < position.y - 0.7f * value || _offTimer > 5f || !Physics.Raycast(val2 + Vector3.up, Vector3.down, ref val3, 2.5f, LayerMask.op_Implicit(SemiFunc.LayerMaskGetVisionObstruct()), (QueryTriggerInteraction)1))
				{
					JumpDown(tp);
					return;
				}
				val2.y = ((RaycastHit)(ref val3)).point.y;
				((Component)_e).transform.position = val2;
				Face(tp, 1f - Mathf.Exp(-10f * dt));
				return;
			}
			MoveTo(tp, Plugin.SprintSpeed.Value);
			_jumpCd -= dt;
			float num = tp.y - position.y;
			Vector3 val4 = Flat(tp - position);
			float magnitude = ((Vector3)(ref val4)).magnitude;
			_stuck = ((magnitude < _lastFlat - 0.05f) ? 0f : (_stuck + dt));
			_lastFlat = Mathf.Min(_lastFlat, magnitude);
			bool flag = (Object)(object)_agent != (Object)null && ((Behaviour)_agent).enabled && !_agent.pathPending && _agent.remainingDistance < 1.2f;
			RaycastHit val5 = default(RaycastHit);
			if (_jumpCd <= 0f && num > 0.8f * value && num < 4.5f * value && magnitude < 6f * value && (flag || _stuck > 0.8f) && Physics.Raycast(tp + Vector3.up * 0.6f, Vector3.down, ref val5, 2f, LayerMask.op_Implicit(SemiFunc.LayerMaskGetVisionObstruct()), (QueryTriggerInteraction)1))
			{
				_lastFlat = float.MaxValue;
				_stuck = 0f;
				StartJump(((RaycastHit)(ref val5)).point, 0.8f * value + num * 0.4f);
			}
		}

		public bool HitTest(Bounds b)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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_005d: 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_0071: 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_0079: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_e != (Object)null && _e.State != FredState.Hidden)
			{
				Bounds val = new Bounds(((Component)_e).transform.position + Vector3.up * 1.1f * Plugin.Scale.Value, new Vector3(1.2f, 2.2f, 1.2f) * Plugin.Scale.Value);
				return ((Bounds)(ref val)).Intersects(b);
			}
			return false;
		}

		public bool ShotHits(Vector3 a, Vector3 b)
		{
			//IL_0033: 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_0042: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: 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_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: 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_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: 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_00a2: 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_00ae: 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_00b1: 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_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: 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_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: 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_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: 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_00ed: 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)
			if ((Object)(object)_e == (Object)null || _e.State == FredState.Hidden)
			{
				return false;
			}
			float value = Plugin.Scale.Value;
			Vector3 val = ((Component)_e).transform.position + Vector3.up * 0.2f * value;
			Vector3 val2 = ((Component)_e).transform.position + Vector3.up * 2.1f * value;
			int num = Mathf.Clamp(Mathf.CeilToInt(Vector3.Distance(a, b) / 0.2f), 1, 400);
			for (int i = 0; i <= num; i++)
			{
				Vector3 val3 = Vector3.Lerp(a, b, (float)i / (float)num);
				Vector3 val4 = val + Vector3.Project(val3 - val, Vector3.up);
				val4.y = Mathf.Clamp(val4.y, val.y, val2.y);
				Vector3 val5 = val3 - val4;
				if (((Vector3)(ref val5)).sqrMagnitude < 0.25f * value * value)
				{
					return true;
				}
			}
			return false;
		}

		public void Damage(int amount, int sourceId)
		{
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			if (!FredNet.IsHost || (Object)(object)_e == (Object)null || _e.State == FredState.Hidden)
			{
				return;
			}
			if (sourceId >= 0)
			{
				if (_hitCd.TryGetValue(sourceId, out var value) && Time.time - value < 0.5f)
				{
					return;
				}
				_hitCd[sourceId] = Time.time;
			}
			_hp -= amount;
			Plugin.Log.LogInfo((object)$"Fredbear hit for {amount}, HP left {_hp}");
			_e.PlayKey("growl", 1f);
			if (_e.State != FredState.Kill)
			{
				_e.Flinch();
				_stagger = 0.35f;
			}
			if (_hp <= 0)
			{
				Stop();
				_e.PlayKey("crunch", 1.3f);
				_target = null;
				_respawn = Plugin.Respawn.Value;
				Enter(FredState.Hidden);
			}
			else
			{
				if (_e.State != FredState.Patrol && _e.State != FredState.Look && _e.State != FredState.Investigate)
				{
					return;
				}
				PlayerAvatar val = null;
				float num = float.MaxValue;
				foreach (PlayerAvatar item in Alive())
				{
					float num2 = Vector3.Distance(((Component)item).transform.position, ((Component)_e).transform.position);
					if (num2 < num)
					{
						num = num2;
						val = item;
					}
				}
				if ((Object)(object)val != (Object)null)
				{
					_target = val;
					_prey = null;
					_lastSeen = ((Component)val).transform.position;
					Enter(FredState.Aggro);
				}
			}
		}

		private void Kill(PlayerAvatar victim)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: 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_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: 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_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: 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_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: 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_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			Stop();
			int num = Random.Range(0, 2);
			Vector3 val = Flat(((Component)victim).transform.position - ((Component)_e).transform.position);
			Quaternion val2 = Quaternion.LookRotation(((Vector3)(ref val)).normalized + ((Component)_e).transform.forward * 0.001f);
			((Component)_e).transform.rotation = val2;
			_stateTime = 0f;
			int num2 = ViewId(victim);
			FredNet.SendKill(num2, num, ((Component)_e).transform.position, val2);
			PlayKill(num2, num, ((Component)_e).transform.position, val2);
			Broadcast();
		}

		public void PlayKill(int victimId, int idx, Vector3 pos, Quaternion rot)
		{
			//IL_0048: 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_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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)
			if ((Object)(object)_e == (Object)null)
			{
				if ((Object)(object)FredAssets.Template == (Object)null)
				{
					return;
				}
				_e = FredEntity.Create(pos, rot);
				_e.Remote = !FredNet.IsHost;
			}
			((Component)_e).transform.SetPositionAndRotation(pos, rot);
			_e.NetPos = pos;
			_e.NetRot = rot;
			_e.StartKill(idx);
			PlayerAvatar val = SemiFunc.PlayerAvatarLocal();
			PlayerAvatar val2 = ((victimId < 0) ? null : (FredNet.Online ? SemiFunc.PlayerAvatarGetFromPhotonID(victimId) : val));
			if ((Object)(object)val2 != (Object)null && (Object)(object)val2 == (Object)(object)val)
			{
				((MonoBehaviour)this).StartCoroutine(LocalDeath(val2, idx));
			}
		}

		private IEnumerator LocalDeath(PlayerAvatar me, int idx)
		{
			float impact = FredEntity.ImpactTime[idx];
			PlayerController instance = PlayerController.instance;
			if (instance != null)
			{
				instance.InputDisable(impact + 0.3f);
			}
			CameraGlitch instance2 = CameraGlitch.Instance;
			if (instance2 != null)
			{
				instance2.PlayLong();
			}
			AudioSource val = default(AudioSource);
			if (!((Component)this).TryGetComponent<AudioSource>(ref val))
			{
				val = ((Component)this).gameObject.AddComponent<AudioSource>();
			}
			val.spatialBlend = 0f;
			AudioClip val2 = FredAssets.Random("scream");
			if ((Object)(object)val2 != (Object)null)
			{
				val.PlayOneShot(val2, 0.9f * Plugin.Volume.Value);
			}
			for (float t = 0f; t < impact; t += Time.deltaTime)
			{
				if ((Object)(object)_e == (Object)null || DeadSet.Invoke(me) || _e.State != FredState.Kill)
				{
					yield break;
				}
				CameraAim instance3 = CameraAim.Instance;
				if (instance3 != null)
				{
					instance3.AimTargetSet(_e.FacePosition, 0.15f, 25f, ((Component)_e).gameObject, 900);
				}
				yield return null;
			}
			SemiFunc.CameraShake(10f, 1.2f);
			if (Plugin.InstantKill.Value && !DeadSet.Invoke(me))
			{
				me.playerHealth.Hurt(9999, false, -1, false);
			}
		}

		private bool Valid(PlayerAvatar p)
		{
			if ((Object)(object)p != (Object)null && !IsDisabled.Invoke(p))
			{
				return !DeadSet.Invoke(p);
			}
			return false;
		}

		private void MoveTo(Vector3 p, float speed)
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: 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_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_005c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_agent == (Object)null)
			{
				return;
			}
			if (!((Behaviour)_agent).enabled)
			{
				((Behaviour)_agent).enabled = true;
			}
			if (!_agent.isOnNavMesh)
			{
				NavMeshHit val = default(NavMeshHit);
				if (!NavMesh.SamplePosition(((Component)_e).transform.position, ref val, 4f, -1))
				{
					return;
				}
				_agent.Warp(((NavMeshHit)(ref val)).position);
			}
			_agent.isStopped = false;
			_agent.speed = speed;
			_moveTimer -= Time.deltaTime;
			if (!(_moveTimer <= 0f))
			{
				Vector3 val2 = _agent.destination - p;
				if (!(((Vector3)(ref val2)).sqrMagnitude > 1f))
				{
					return;
				}
			}
			_moveTimer = 0.25f;
			_agent.SetDestination(p);
		}

		private void Stop()
		{
			if ((Object)(object)_agent != (Object)null && ((Behaviour)_agent).enabled && _agent.isOnNavMesh)
			{
				_agent.isStopped = true;
			}
		}

		private bool Arrived()
		{
			if (!((Object)(object)_agent == (Object)null) && ((Behaviour)_agent).enabled && _agent.isOnNavMesh)
			{
				if (!_agent.pathPending)
				{
					return _agent.remainingDistance < 0.8f;
				}
				return false;
			}
			return true;
		}

		private void Face(Vector3 p, float t)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//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_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_0040: 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_0046: 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)
			Vector3 val = Flat(p - ((Component)_e).transform.position);
			if (((Vector3)(ref val)).sqrMagnitude > 0.01f)
			{
				((Component)_e).transform.rotation = Quaternion.Slerp(((Component)_e).transform.rotation, Quaternion.LookRotation(val), t);
			}
		}

		private static Vector3 Flat(Vector3 v)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			v.y = 0f;
			return v;
		}
	}
	public enum FredState : byte
	{
		Hidden,
		Patrol,
		Look,
		Investigate,
		Aggro,
		Chase,
		Kill
	}
	internal sealed class FredEntity : MonoBehaviour
	{
		public Vector3? LookAt;

		public bool Remote;

		public Vector3 NetPos;

		public Quaternion NetRot = Quaternion.identity;

		public Vector3 Velocity;

		public int KillIndex;

		private const float WalkNative = 0.78f;

		private const float SprintNative = 3.96f;

		public static readonly string[] KillClips = new string[2] { "Kill_Bite", "Kill_Slam" };

		public static readonly float[] ImpactTime = new float[2] { 1.5333333f, 1.6666666f };

		public const float ScreamTime = 1.2f;

		private static readonly Dictionary<string, (float t, string key, float vol)[]> Events = new Dictionary<string, (float, string, float)[]>
		{
			["Search_Walk"] = new(float, string, float)[2]
			{
				(1f / 30f, "step", 0.8f),
				(2f / 3f, "step", 0.8f)
			},
			["Spotted_Scream"] = new(float, string, float)[1] { (0.2f, "spotted", 1.4f) },
			["Search_LookAround"] = new(float, string, float)[5]
			{
				(0.5f, "servo", 0.6f),
				(1.6666666f, "servo", 0.6f),
				(2.5f, "servo", 0.6f),
				(3.6666667f, "servo", 0.6f),
				(4.4f, "servo", 0.6f)
			},
			["Kill_Bite"] = new(float, string, float)[4]
			{
				(7f / 15f, "growl", 1f),
				(1.5333333f, "bite", 1.2f),
				(1.5333333f, "slam", 0.6f),
				(1.9333333f, "crunch", 1.2f)
			},
			["Kill_Slam"] = new(float, string, float)[4]
			{
				(0.4f, "growl", 1f),
				(2f / 3f, "aggro", 1f),
				(1.6666666f, "slam", 1.3f),
				(2.0666666f, "crunch", 1.2f)
			}
		};

		private readonly Dictionary<string, Transform> _bones = new Dictionary<string, Transform>();

		private readonly Dictionary<Transform, Vector3> _restPos = new Dictionary<Transform, Vector3>();

		private readonly Dictionary<Transform, Quaternion> _restRot = new Dictionary<Transform, Quaternion>();

		private readonly Dictionary<Transform, Quaternion> _snapRot = new Dictionary<Transform, Quaternion>();

		private readonly Dictionary<Transform, Vector3> _snapPos = new Dictionary<Transform, Vector3>();

		private Transform _head;

		private Vector3 _faceLocal;

		private Renderer[] _renderers;

		private FredClip _clip;

		private float _time;

		private float _prevT;

		private float _fade = 1f;

		private float _fadeDur = 0.2f;

		private float _speed = 1f;

		private Quaternion _look = Quaternion.identity;

		private Vector3 _lastPos;

		private AudioSource _voice;

		private AudioSource _feet;

		private AudioSource _hum;

		private AudioSource _run;

		private Light _spot;

		private Light _glow;

		private float _light;

		private float _flinch;

		public FredState State { get; private set; } = FredState.Patrol;

		public Vector3 FacePosition => _head.position + _head.rotation * _faceLocal * 0.15f;

		public Vector3 EyePosition => _head.position;

		public void Flinch()
		{
			_flinch = 0.35f;
		}

		public static FredEntity Create(Vector3 pos, Quaternion rot)
		{
			//IL_0005: 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)
			//IL_0012: 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)
			GameObject obj = Object.Instantiate<GameObject>(FredAssets.Template, pos, rot);
			obj.transform.localScale = Vector3.one * Plugin.Scale.Value;
			((Object)obj).name = "NightmareFredbear(Instance)";
			obj.SetActive(true);
			return obj.AddComponent<FredEntity>();
		}

		private void Awake()
		{
			//IL_0040: 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_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_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b8: Expected O, but got Unknown
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: 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_01f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0207: Unknown result type (might be due to invalid IL or missing references)
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0211: 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_02a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a9: Expected O, but got Unknown
			//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0303: Unknown result type (might be due to invalid IL or missing references)
			//IL_036d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0377: 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_03a5: Unknown result type (might be due to invalid IL or missing references)
			Transform[] componentsInChildren = ((Component)this).GetComponentsInChildren<Transform>(true);
			foreach (Transform val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)(object)((Component)this).transform))
				{
					_bones[((Object)val).name] = val;
					_restPos[val] = val.localPosition;
					_restRot[val] = val.localRotation;
				}
			}
			_head = (_bones.TryGetValue("Head_jnt_06", out var value) ? value : ((Component)this).transform);
			_faceLocal = Quaternion.Inverse(_head.rotation) * ((Component)this).transform.forward;
			_renderers = ((Component)this).GetComponentsInChildren<Renderer>(true);
			_voice = NewSource(((Component)this).gameObject, 3f, 30f);
			_feet = NewSource(((Component)this).gameObject, 1.5f, 18f);
			_hum = NewSource(((Component)this).gameObject, 1f, 8f);
			_hum.loop = true;
			_hum.clip = FredAssets.Random("hum");
			if ((Object)(object)_hum.clip != (Object)null)
			{
				_hum.volume = 0f;
				_hum.Play();
			}
			_run = NewSource(((Component)this).gameObject, 2f, 22f);
			_run.loop = true;
			_run.clip = FredAssets.Random("run");
			_run.volume = 0f;
			GameObject val2 = new GameObject("FredSpot");
			val2.transform.SetParent(_head, false);
			val2.transform.localPosition = _faceLocal * 0.2f;
			val2.transform.localRotation = Quaternion.LookRotation(_faceLocal, Quaternion.Inverse(_head.rotation) * ((Component)this).transform.up);
			_spot = val2.AddComponent<Light>();
			_spot.type = (LightType)0;
			_spot.color = new Color(1f, 0.06f, 0.04f);
			_spot.range = 13f;
			_spot.spotAngle = 62f;
			_spot.innerSpotAngle = 25f;
			_spot.shadows = (LightShadows)0;
			_spot.intensity = 0f;
			GameObject val3 = new GameObject("FredGlow");
			val3.transform.SetParent(_head, false);
			val3.transform.localPosition = _faceLocal * 0.25f;
			_glow = val3.AddComponent<Light>();
			_glow.type = (LightType)2;
			_glow.color = new Color(1f, 0.1f, 0.05f);
			_glow.range = 2.5f;
			_glow.shadows = (LightShadows)0;
			_glow.intensity = 0f;
			((Component)this).gameObject.layer = 2;
			CapsuleCollider obj = ((Component)this).gameObject.AddComponent<CapsuleCollider>();
			((Collider)obj).isTrigger = true;
			obj.radius = 0.45f;
			obj.height = 2.2f;
			obj.center = Vector3.up * 1.1f;
			Rigidbody obj2 = ((Component)this).gameObject.AddComponent<Rigidbody>();
			obj2.isKinematic = true;
			obj2.useGravity = false;
			_lastPos = ((Component)this).transform.position;
			Play("Search_LookAround", 0f);
		}

		private void OnTriggerEnter(Collider other)
		{
			//IL_0028: 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)
			Rigidbody attachedRigidbody = other.attachedRigidbody;
			if (!((Object)(object)attachedRigidbody == (Object)null) && !attachedRigidbody.isKinematic && !((Object)(object)((Component)other).GetComponentInParent<PlayerAvatar>() != (Object)null))
			{
				Vector3 linearVelocity = attachedRigidbody.linearVelocity;
				float magnitude = ((Vector3)(ref linearVelocity)).magnitude;
				if (!(magnitude < 4f))
				{
					FredDirector.Instance?.Damage(Mathf.Clamp(Mathf.RoundToInt(attachedRigidbody.mass * magnitude * 3f), 5, 100), ((Object)attachedRigidbody).GetInstanceID());
				}
			}
		}

		private static AudioSource NewSource(GameObject go, float min, float max)
		{
			AudioSource obj = go.AddComponent<AudioSource>();
			obj.spatialBlend = 1f;
			obj.rolloffMode = (AudioRolloffMode)1;
			obj.minDistance = min;
			obj.maxDistance = max;
			obj.dopplerLevel = 0f;
			obj.playOnAwake = false;
			return obj;
		}

		public void SetState(FredState s)
		{
			if (s != State)
			{
				State = s;
				Renderer[] renderers = _renderers;
				for (int i = 0; i < renderers.Length; i++)
				{
					renderers[i].enabled = s != FredState.Hidden;
				}
			}
		}

		public void StartKill(int index)
		{
			KillIndex = Mathf.Clamp(index, 0, 1);
			SetState(FredState.Kill);
			_clip = null;
			Play(KillClips[KillIndex], 0.08f);
		}

		public void PlayKey(string key, float vol)
		{
			AudioClip val = FredAssets.Random(key);
			if (!((Object)(object)val == (Object)null))
			{
				AudioSource obj = ((key == "step" || key == "stomp") ? _feet : _voice);
				obj.pitch = Random.Range(0.92f, 1.06f);
				obj.PlayOneShot(val, vol * Plugin.Volume.Value);
			}
		}

		public void Play(string name, float fade)
		{
			//IL_0043: 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)
			if (!FredAssets.Clips.TryGetValue(name, out var value) || value == _clip)
			{
				return;
			}
			foreach (KeyValuePair<Transform, Quaternion> item in _restRot)
			{
				_snapRot[item.Key] = item.Key.localRotation;
				_snapPos[item.Key] = item.Key.localPosition;
			}
			_clip = value;
			_time = 0f;
			_prevT = -1f;
			_fade = ((fade <= 0f) ? 1f : 0f);
			_fadeDur = Mathf.Max(fade, 0.01f);
		}

		private void LateUpdate()
		{
			//IL_00a1: 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_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_00bd: 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_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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_0044: 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_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: 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_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: 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_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02de: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e3: Unknown result type (might be due to invalid IL or missing references)
			float deltaTime = Time.deltaTime;
			if (deltaTime <= 0f)
			{
				return;
			}
			Vector3 val;
			if (Remote)
			{
				float num = 1f - Mathf.Exp(-12f * deltaTime);
				Transform transform = ((Component)this).transform;
				val = ((Component)this).transform.position - NetPos;
				transform.position = ((((Vector3)(ref val)).sqrMagnitude > 25f) ? NetPos : Vector3.Lerp(((Component)this).transform.position, NetPos, num));
				((Component)this).transform.rotation = Quaternion.Slerp(((Component)this).transform.rotation, NetRot, num);
			}
			Velocity = Vector3.Lerp(Velocity, (((Component)this).transform.position - _lastPos) / deltaTime, 1f - Mathf.Exp(-8f * deltaTime));
			_lastPos = ((Component)this).transform.position;
			Vector3 val2 = default(Vector3);
			((Vector3)(ref val2))..ctor(Velocity.x, 0f, Velocity.z);
			if (!Remote && State != FredState.Kill && State != FredState.Aggro && ((Vector3)(ref val2)).sqrMagnitude > 0.04f)
			{
				((Component)this).transform.rotation = Quaternion.Slerp(((Component)this).transform.rotation, Quaternion.LookRotation(val2), 1f - Mathf.Exp((0f - ((State == FredState.Chase) ? 10f : 4f)) * deltaTime));
			}
			Lights(deltaTime);
			int num2;
			if (State == FredState.Chase)
			{
				val = new Vector3(Velocity.x, 0f, Velocity.z);
				num2 = ((((Vector3)(ref val)).sqrMagnitude > 0.25f) ? 1 : 0);
			}
			else
			{
				num2 = 0;
			}
			bool flag = (byte)num2 != 0;
			if (flag && !_run.isPlaying && (Object)(object)_run.clip != (Object)null)
			{
				_run.Play();
			}
			_run.volume = Mathf.MoveTowards(_run.volume, flag ? Plugin.Volume.Value : 0f, (flag ? 6f : 2f) * deltaTime);
			if (!flag && _run.volume <= 0f && _run.isPlaying)
			{
				_run.Stop();
			}
			if (State == FredState.Hidden)
			{
				_hum.volume = 0f;
				return;
			}
			ChooseClip();
			Sample(deltaTime);
			HeadLook(deltaTime);
			if (_flinch > 0f)
			{
				_flinch -= deltaTime;
				if (_bones.TryGetValue("Spine_jnt_04", out var value))
				{
					value.rotation = Quaternion.AngleAxis(-28f * _flinch / 0.35f, ((Component)this).transform.right) * value.rotation;
				}
			}
			_hum.volume = Mathf.MoveTowards(_hum.volume, ((State == FredState.Chase || State == FredState.Kill) ? 0.15f : 0.4f) * Plugin.Volume.Value, deltaTime);
		}

		private void ChooseClip()
		{
			//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)
			Vector3 val = new Vector3(Velocity.x, 0f, Velocity.z);
			float magnitude = ((Vector3)(ref val)).magnitude;
			switch (State)
			{
			case FredState.Kill:
				_speed = 1f;
				break;
			case FredState.Aggro:
				Play("Spotted_Scream", 0.1f);
				_speed = 1f;
				break;
			case FredState.Chase:
				if (magnitude > 0.5f)
				{
					Play("Sprint", 0.12f);
					_speed = Mathf.Clamp(magnitude / (3.96f * Plugin.Scale.Value), 0.6f, 1.9f);
				}
				else
				{
					Play("Search_LookAround", 0.3f);
					_speed = 1f;
				}
				break;
			default:
				if (magnitude > 0.25f)
				{
					Play("Search_Walk", 0.3f);
					_speed = Mathf.Clamp(magnitude / (0.78f * Plugin.Scale.Value), 0.5f, 1.9f);
				}
				else
				{
					Play("Search_LookAround", 0.4f);
					_speed = 1f;
				}
				break;
			}
		}

		private void Sample(float dt)
		{
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: 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_01ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0241: Unknown result type (might be due to invalid IL or missing references)
			//IL_024d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0253: Unknown result type (might be due to invalid IL or missing references)
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0283: Unknown result type (might be due to invalid IL or missing references)
			if (_clip == null)
			{
				return;
			}
			_time += dt * _speed;
			float num = (_clip.Loop ? Mathf.Repeat(_time, _clip.Length) : Mathf.Min(_time, _clip.Length));
			FireEvents(_prevT, num);
			_prevT = num;
			_fade = Mathf.Min(1f, _fade + dt / _fadeDur);
			float num2 = Mathf.SmoothStep(0f, 1f, _fade);
			foreach (KeyValuePair<Transform, Vector3> restPo in _restPos)
			{
				restPo.Key.localPosition = restPo.Value;
			}
			foreach (FredClip.Track track in _clip.Tracks)
			{
				if (_bones.TryGetValue(track.Bone, out var value))
				{
					int num3 = Array.BinarySearch(track.Times, num);
					if (num3 < 0)
					{
						num3 = ~num3 - 1;
					}
					num3 = Mathf.Clamp(num3, 0, track.Times.Length - 1);
					int num4 = Mathf.Min(num3 + 1, track.Times.Length - 1);
					float num5 = ((num4 == num3) ? 0f : Mathf.Clamp01((num - track.Times[num3]) / (track.Times[num4] - track.Times[num3])));
					if (track.Rotation)
					{
						value.localRotation = Quaternion.Slerp(track.Rot[num3], track.Rot[num4], num5);
					}
					else
					{
						value.localPosition = Vector3.Lerp(track.Pos[num3], track.Pos[num4], num5);
					}
				}
			}
			if (!(num2 < 1f))
			{
				return;
			}
			foreach (KeyValuePair<Transform, Quaternion> item in _snapRot)
			{
				item.Key.localRotation = Quaternion.Slerp(item.Value, item.Key.localRotation, num2);
				item.Key.localPosition = Vector3.Lerp(_snapPos[item.Key], item.Key.localPosition, num2);
			}
		}

		private void FireEvents(float from, float to)
		{
			if (_speed < 0.3f || !Events.TryGetValue(_clip.Name, out (float, string, float)[] value))
			{
				return;
			}
			(float, string, float)[] array = value;
			for (int i = 0; i < array.Length; i++)
			{
				(float, string, float) tuple = array[i];
				bool num;
				if (!(to >= from))
				{
					if (tuple.Item1 > from)
					{
						goto IL_006d;
					}
					num = tuple.Item1 <= to;
				}
				else
				{
					if (!(tuple.Item1 > from))
					{
						continue;
					}
					num = tuple.Item1 <= to;
				}
				if (!num)
				{
					continue;
				}
				goto IL_006d;
				IL_006d:
				PlayKey(tuple.Item2, tuple.Item3);
			}
		}

		private void HeadLook(float dt)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: 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_00b4: Unknown result type (might be due to invalid IL or missing references)
			//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_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: 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_0038: 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_0052: 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_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: 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)
			Quaternion val = Quaternion.identity;
			if (LookAt.HasValue && State != FredState.Kill)
			{
				val = Quaternion.FromToRotation(_head.rotation * _faceLocal, LookAt.Value - _head.position);
				float num = default(float);
				Vector3 val2 = default(Vector3);
				((Quaternion)(ref val)).ToAngleAxis(ref num, ref val2);
				if (num > 180f)
				{
					num = 360f - num;
				}
				if (num > 60f)
				{
					val = Quaternion.Slerp(Quaternion.identity, val, 60f / num);
				}
			}
			_look = Quaternion.Slerp(_look, val, 1f - Mathf.Exp(-6f * dt));
			_head.rotation = _look * _head.rotation;
		}

		private void Lights(float dt)
		{
			float num = ((State == FredState.Hidden) ? 0f : ((State == FredState.Kill) ? 9f : ((State == FredState.Chase || State == FredState.Aggro) ? 6.5f : 3.5f)));
			_light = Mathf.MoveTowards(_light, num, 12f * dt);
			float num2 = ((State == FredState.Chase) ? (0.85f + 0.15f * Mathf.PerlinNoise(Time.time * 9f, 0f)) : 1f);
			_spot.intensity = _light * num2 * Plugin.LightIntensity.Value;
			_glow.intensity = _light * 0.35f * Plugin.LightIntensity.Value;
			Light spot = _spot;
			bool enabled = (((Behaviour)_glow).enabled = _light > 0.01f);
			((Behaviour)spot).enabled = enabled;
		}
	}
	internal static class FredNet
	{
		private static NetworkedEvent _state;

		private static NetworkedEvent _kill;

		public static bool Online
		{
			get
			{
				if (SemiFunc.IsMultiplayer())
				{
					return PhotonNetwork.InRoom;
				}
				return false;
			}
		}

		public static bool IsHost => SemiFunc.IsMasterClientOrSingleplayer();

		public static void Register()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing reference