Decompiled source of ShipTurret v0.1.0

ShipTurret.dll

Decompiled an hour ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
using ModMenu;
using TMPro;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.UI;

[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("ShipTurret")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+89bcf738352c01ba9422d7fd995434e006e3fbc9")]
[assembly: AssemblyProduct("ShipTurret")]
[assembly: AssemblyTitle("ShipTurret")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ShipTurret
{
	internal static class Art
	{
		internal const float BaseTop = 0.4f;

		internal const float Axle = 0.42f;

		internal const float Scale = 1.3f;

		internal static readonly Vector3 Cam = new Vector3(0f, 0.2f, 0.2f);

		internal static readonly Vector3 Muzzle = new Vector3(0f, -0.21f, 0.45f);

		internal static readonly Vector3 LensFront = new Vector3(0f, 0f, 0.26f);

		internal const float FootHalf = 0.4f;

		internal const float SweepRadius = 0.5f;

		internal const float Height = 1.05f;

		internal static readonly Dictionary<string, Mesh> Meshes = new Dictionary<string, Mesh>();

		internal static Texture2D Palette;

		internal static void Load()
		{
			try
			{
				ReadMeshes(Res("turret.bin"));
				Palette = Tex("palette");
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Ship Turret: art failed to load: " + ex));
			}
			string[] array = new string[5] { "Base", "Yaw", "Head", "Lens", "Coil" };
			foreach (string text in array)
			{
				if (!Meshes.ContainsKey(text))
				{
					Plugin.Log.LogError((object)("Ship Turret: mesh " + text + " missing from turret.bin."));
				}
			}
		}

		private static Stream Res(string name)
		{
			return typeof(Art).Assembly.GetManifestResourceStream("ShipTurret." + name);
		}

		private static void ReadMeshes(Stream s)
		{
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: 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_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Expected O, but got Unknown
			if (s == null)
			{
				Plugin.Log.LogError((object)"Ship Turret: turret.bin is missing.");
				return;
			}
			using (s)
			{
				using GZipStream input = new GZipStream(s, CompressionMode.Decompress);
				using BinaryReader binaryReader = new BinaryReader(input);
				if (Encoding.ASCII.GetString(binaryReader.ReadBytes(4)) != "BBD1")
				{
					Plugin.Log.LogError((object)"Ship Turret: turret.bin isn't BBD1.");
					return;
				}
				int num = binaryReader.ReadInt32();
				for (int i = 0; i < num; i++)
				{
					string text = Encoding.UTF8.GetString(binaryReader.ReadBytes(binaryReader.ReadInt32()));
					int num2 = binaryReader.ReadInt32();
					int num3 = binaryReader.ReadInt32();
					Vector3[] array = (Vector3[])(object)new Vector3[num2];
					Vector3[] array2 = (Vector3[])(object)new Vector3[num2];
					Vector2[] array3 = (Vector2[])(object)new Vector2[num2];
					for (int j = 0; j < num2; j++)
					{
						array[j] = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
						array2[j] = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
						array3[j] = new Vector2(binaryReader.ReadSingle(), binaryReader.ReadSingle());
					}
					int[] array4 = new int[num3];
					for (int k = 0; k < num3; k++)
					{
						array4[k] = binaryReader.ReadUInt16();
					}
					Mesh val = new Mesh
					{
						name = "ShipTurret." + text
					};
					val.vertices = array;
					val.normals = array2;
					val.uv = array3;
					val.triangles = array4;
					val.RecalculateBounds();
					val.RecalculateTangents();
					val.UploadMeshData(true);
					Meshes[text] = val;
				}
			}
		}

		private static Texture2D Tex(string name)
		{
			//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_0059: 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_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			using Stream stream = Res(name + ".png");
			if (stream == null)
			{
				Plugin.Log.LogError((object)("Ship Turret: texture " + name + " is missing."));
				return null;
			}
			MemoryStream memoryStream = new MemoryStream();
			stream.CopyTo(memoryStream);
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, true)
			{
				name = "ShipTurret." + name,
				wrapMode = (TextureWrapMode)1,
				anisoLevel = 4
			};
			ImageConversion.LoadImage(val, memoryStream.ToArray(), false);
			((Texture)val).filterMode = (FilterMode)2;
			return val;
		}
	}
	internal static class Sounds
	{
		internal static AudioClip Zap;

		internal static AudioClip Wake;

		internal static AudioClip Sleep;

		internal static AudioClip Charge;

		internal static AudioClip Click;

		internal static AudioClip Flicker;

		private static bool _found;

		internal static void Find()
		{
			if (_found)
			{
				return;
			}
			StartOfRound instance = StartOfRound.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			_found = true;
			try
			{
				ItemCharger val = Object.FindObjectOfType<ItemCharger>();
				if ((Object)(object)val != (Object)null && (Object)(object)val.zapAudio != (Object)null)
				{
					Zap = val.zapAudio.clip;
				}
				SelectableLevel[] levels = instance.levels;
				foreach (SelectableLevel val2 in levels)
				{
					if ((Object)(object)val2 == (Object)null || val2.spawnableMapObjects == null)
					{
						continue;
					}
					SpawnableMapObject[] spawnableMapObjects = val2.spawnableMapObjects;
					foreach (SpawnableMapObject val3 in spawnableMapObjects)
					{
						Turret val4 = (((Object)(object)val3?.prefabToSpawn != (Object)null) ? val3.prefabToSpawn.GetComponentInChildren<Turret>(true) : null);
						if (!((Object)(object)val4 == (Object)null))
						{
							Wake = val4.turretActivate;
							Sleep = val4.turretDeactivate;
							Charge = val4.chargingSFX;
							break;
						}
					}
					if ((Object)(object)Wake != (Object)null)
					{
						break;
					}
				}
				FlashlightItem val5 = Flashlights.Pro();
				if ((Object)(object)val5 != (Object)null)
				{
					if (val5.flashlightClips != null && val5.flashlightClips.Length != 0)
					{
						Click = val5.flashlightClips[0];
					}
					Flicker = val5.flashlightFlicker;
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Ship Turret: couldn't find all the game's sounds: " + ex.Message));
			}
			Plugin.Debug($"sounds: zap {(Object)(object)Zap != (Object)null}, wake {(Object)(object)Wake != (Object)null}, charge {(Object)(object)Charge != (Object)null}, click {(Object)(object)Click != (Object)null}");
		}
	}
	internal static class Flashlights
	{
		internal static FlashlightItem Pro()
		{
			StartOfRound instance = StartOfRound.Instance;
			if ((Object)(object)instance == (Object)null || (Object)(object)instance.allItemsList == (Object)null)
			{
				return null;
			}
			FlashlightItem val = null;
			foreach (Item items in instance.allItemsList.itemsList)
			{
				FlashlightItem val2 = (((Object)(object)items != (Object)null && (Object)(object)items.spawnPrefab != (Object)null) ? items.spawnPrefab.GetComponent<FlashlightItem>() : null);
				if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val2.flashlightBulb == (Object)null))
				{
					if (items.itemName != null && items.itemName.ToLowerInvariant().Contains("pro"))
					{
						return val2;
					}
					if (val == null)
					{
						val = val2;
					}
				}
			}
			return val;
		}
	}
	internal static class Hooks
	{
		internal const string NewShipGuid = "daniel.lethalcompany.newship";

		private static Func<GameObject> _hull;

		internal static GameObject Hull
		{
			get
			{
				try
				{
					return _hull?.Invoke();
				}
				catch (Exception)
				{
					return null;
				}
			}
		}

		internal static void Init()
		{
			if (Chainloader.PluginInfos.TryGetValue("daniel.lethalcompany.newship", out var value))
			{
				Type type = ((object)value.Instance).GetType().Assembly.GetType("NewShip.Applier");
				MethodInfo methodInfo = ((type != null) ? AccessTools.PropertyGetter(type, "Instance") : null);
				if (methodInfo != null)
				{
					_hull = (Func<GameObject>)methodInfo.CreateDelegate(typeof(Func<GameObject>));
				}
				else
				{
					Plugin.Log.LogWarning((object)"Ship Turret: couldn't read NewShip's fitted hull; the turret only looks for the roof when it's built.");
				}
			}
		}
	}
	internal sealed class TurretState
	{
		public float Yaw;

		public float Pitch;

		public bool Light;

		public bool Overheated;

		public float Heat;

		public float Recharge = 1f;

		public ulong Operator = ulong.MaxValue;
	}
	internal static class Host
	{
		internal const float PitchMin = -30f;

		internal const float PitchMax = 55f;

		internal const float LightCone = 15f;

		internal const float ArcCone = 7f;

		internal static TurretState State = new TurretState();

		internal static bool Dirty;

		private static float _readyAt;

		private static float _nextSend;

		private static float _nextDazzle;

		private static float _operatorSeen;

		private static readonly Dictionary<ulong, float> _exposure = new Dictionary<ulong, float>();

		private static readonly Dictionary<ulong, float> _dazzledUntil = new Dictionary<ulong, float>();

		private static byte _sentHeat = byte.MaxValue;

		private static byte _sentRecharge = byte.MaxValue;

		internal static void OnSessionStart()
		{
			State = new TurretState();
			Dirty = true;
			_readyAt = 0f;
			_nextSend = 0f;
			_nextDazzle = 0f;
			_exposure.Clear();
			_dazzledUntil.Clear();
		}

		internal static void Tick()
		{
			float time = Time.time;
			float deltaTime = Time.deltaTime;
			TurretState state = State;
			if (state.Heat > 0f)
			{
				state.Heat = Mathf.Max(0f, state.Heat - Plugin.Cooling.Value * deltaTime);
				if (state.Heat <= 0f && state.Overheated)
				{
					state.Overheated = false;
					Dirty = true;
					Plugin.Debug("cooled down");
				}
			}
			state.Recharge = ((_readyAt > time) ? Mathf.Clamp01(1f - (_readyAt - time) / Mathf.Max(0.01f, Plugin.ArcRecharge.Value)) : 1f);
			byte b = (byte)Mathf.RoundToInt(state.Heat * 2.55f);
			byte b2 = (byte)Mathf.RoundToInt(state.Recharge * 255f);
			if (b != _sentHeat || b2 != _sentRecharge)
			{
				Dirty = true;
			}
			if (state.Operator != ulong.MaxValue && time - _operatorSeen > 2.5f)
			{
				state.Operator = ulong.MaxValue;
				Dirty = true;
			}
			if (time >= _nextDazzle)
			{
				_nextDazzle = time + 0.2f;
				Dazzle(0.2f);
			}
			if (Dirty && time >= _nextSend)
			{
				_nextSend = time + 0.1f;
				Dirty = false;
				_sentHeat = b;
				_sentRecharge = b2;
				Net.Broadcast();
			}
		}

		internal static void SetAim(ulong from, float yaw, float pitch)
		{
			TurretState state = State;
			yaw = Mathf.Repeat(yaw, 360f);
			pitch = Mathf.Clamp(pitch, -30f, 55f);
			if (Mathf.Abs(Mathf.DeltaAngle(state.Yaw, yaw)) > 0.05f || Mathf.Abs(state.Pitch - pitch) > 0.05f || state.Operator != from)
			{
				Dirty = true;
			}
			state.Yaw = yaw;
			state.Pitch = pitch;
			state.Operator = from;
			_operatorSeen = Time.time;
		}

		internal static string Command(ulong from, byte cmd)
		{
			if (!Store.Unlocked || !Turret.Built)
			{
				return "TURRET OFFLINE";
			}
			switch (cmd)
			{
			case 1:
				State.Light = !State.Light;
				Dirty = true;
				Plugin.Debug("spotlight " + (State.Light ? "on" : "off"));
				return null;
			case 2:
				return Fire(free: false);
			default:
				return null;
			}
		}

		internal static void SetLight(bool on)
		{
			if (State.Light != on)
			{
				State.Light = on;
				Dirty = true;
			}
		}

		internal static string CoolNow()
		{
			State.Heat = 0f;
			State.Overheated = false;
			_readyAt = 0f;
			Dirty = true;
			return "turret cooled and recharged";
		}

		internal static IEnumerable<EnemyAI> Outdoor()
		{
			RoundManager instance = RoundManager.Instance;
			if ((Object)(object)instance == (Object)null || instance.SpawnedEnemies == null)
			{
				yield break;
			}
			foreach (EnemyAI spawnedEnemy in instance.SpawnedEnemies)
			{
				if ((Object)(object)spawnedEnemy != (Object)null && spawnedEnemy.isOutside && !spawnedEnemy.isEnemyDead && (Object)(object)spawnedEnemy.enemyType != (Object)null && spawnedEnemy.enemyType.canBeStunned)
				{
					yield return spawnedEnemy;
				}
			}
		}

		internal static Vector3 Centre(EnemyAI e)
		{
			//IL_002a: 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_003f: 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)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)e.eye != (Object)null))
			{
				return ((Component)e).transform.position + Vector3.up;
			}
			return Vector3.Lerp(((Component)e).transform.position, e.eye.position, 0.6f);
		}

		private static bool Clear(Vector3 a, Vector3 b)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			return !Physics.Linecast(a, b, StartOfRound.Instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1);
		}

		internal static string Fire(bool free)
		{
			//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_00a1: 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_00a4: 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_00c3: 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_00cd: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: 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_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0201: Unknown result type (might be due to invalid IL or missing references)
			StartOfRound instance = StartOfRound.Instance;
			TurretState state = State;
			if ((Object)(object)instance == (Object)null || !Turret.Built)
			{
				return "TURRET OFFLINE";
			}
			if (instance.inShipPhase)
			{
				return "NO TARGETS IN ORBIT";
			}
			if (state.Overheated)
			{
				return "OVERHEATED";
			}
			if (!free && Time.time < _readyAt)
			{
				return "ARC CHARGING";
			}
			Turret.Ray(state.Yaw, state.Pitch, out var muzzle, out var dir);
			float value = Plugin.ArcRange.Value;
			EnemyAI val = null;
			float num = float.MaxValue;
			foreach (EnemyAI item in Outdoor())
			{
				Vector3 val2 = Centre(item);
				Vector3 val3 = val2 - muzzle;
				float magnitude = ((Vector3)(ref val3)).magnitude;
				if (!(magnitude > value) && !(magnitude < 0.3f))
				{
					float num2 = Vector3.Angle(dir, val3);
					Vector3 val4 = Vector3.Cross(dir, val3);
					float magnitude2 = ((Vector3)(ref val4)).magnitude;
					if ((!(num2 > 7f) || !(magnitude2 > 1.2f)) && !(Vector3.Dot(dir, val3) <= 0f) && Clear(muzzle, val2) && num2 < num)
					{
						num = num2;
						val = item;
					}
				}
			}
			RaycastHit val6 = default(RaycastHit);
			Vector3 val5 = (((Object)(object)val != (Object)null) ? Centre(val) : ((!Physics.Raycast(muzzle, dir, ref val6, value, instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1)) ? (muzzle + dir * Mathf.Min(value, 14f)) : ((RaycastHit)(ref val6)).point));
			List<ulong> list = new List<ulong>();
			if ((Object)(object)val != (Object)null)
			{
				list.Add(((NetworkBehaviour)val).NetworkObjectId);
			}
			float value2 = Plugin.ArcSplash.Value;
			if (value2 > 0f)
			{
				foreach (EnemyAI item2 in Outdoor())
				{
					if ((Object)(object)item2 != (Object)(object)val && Vector3.Distance(Centre(item2), val5) <= value2 && Clear(val5 + Vector3.up * 0.3f, Centre(item2)))
					{
						list.Add(((NetworkBehaviour)item2).NetworkObjectId);
					}
				}
			}
			state.Heat = Mathf.Min(100f, state.Heat + Plugin.ArcHeat.Value);
			if (state.Heat >= 100f && !state.Overheated)
			{
				state.Overheated = true;
				Plugin.Debug("overheated");
			}
			if (!free)
			{
				_readyAt = Time.time + Plugin.ArcRecharge.Value;
			}
			Dirty = true;
			Plugin.Debug(string.Format("arc {0}, {1} stunned, heat {2:0}", ((Object)(object)val != (Object)null) ? ("hit " + val.enemyType.enemyName) : "missed", list.Count, state.Heat));
			Net.SendZap(val5, (Object)(object)val != (Object)null, list, Plugin.ArcStun.Value);
			return null;
		}

		internal static string FireAtNearest()
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			if (!Turret.Built)
			{
				return "the turret isn't built";
			}
			Turret.Ray(State.Yaw, State.Pitch, out var muzzle, out var _);
			EnemyAI val = (from x in Outdoor()
				where Clear(muzzle, Centre(x))
				orderby Vector3.Distance(muzzle, Centre(x))
				select x).FirstOrDefault();
			if ((Object)(object)val == (Object)null)
			{
				return "no outdoor monster in sight";
			}
			Turret.AimAt(Centre(val), out var yaw, out var pitch);
			SetAim(NetworkId.Local, yaw, pitch);
			State.Operator = ulong.MaxValue;
			return Fire(free: true) ?? ("fired at " + val.enemyType.enemyName);
		}

		private static void Dazzle(float step)
		{
			//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_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: 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_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_00e9: 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_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			StartOfRound instance = StartOfRound.Instance;
			if (!State.Light || !Plugin.Blinds.Value || (Object)(object)instance == (Object)null || instance.inShipPhase || !Turret.Built)
			{
				_exposure.Clear();
				return;
			}
			Turret.Ray(State.Yaw, State.Pitch, out var _, out var dir);
			Vector3 val = Turret.LensWorld(State.Yaw, State.Pitch);
			float time = Time.time;
			float value = Plugin.LightRange.Value;
			List<ulong> list = new List<ulong>();
			HashSet<ulong> seen = new HashSet<ulong>();
			foreach (EnemyAI item in Outdoor())
			{
				ulong networkObjectId = ((NetworkBehaviour)item).NetworkObjectId;
				Vector3 val2 = Centre(item);
				Vector3 val3 = val2 - val;
				if (!(((Vector3)(ref val3)).magnitude > value) && !(Vector3.Angle(dir, val3) > 15f) && Clear(val, val2))
				{
					seen.Add(networkObjectId);
					_exposure.TryGetValue(networkObjectId, out var value2);
					value2 += step;
					_dazzledUntil.TryGetValue(networkObjectId, out var value3);
					if (value2 >= Plugin.BlindAfter.Value && time >= value3)
					{
						list.Add(networkObjectId);
						_dazzledUntil[networkObjectId] = time + Plugin.BlindCooldown.Value;
						value2 = 0f;
						Plugin.Debug("dazzled " + item.enemyType.enemyName);
					}
					_exposure[networkObjectId] = value2;
				}
			}
			foreach (ulong item2 in _exposure.Keys.Where((ulong k) => !seen.Contains(k)).ToList())
			{
				_exposure.Remove(item2);
			}
			if (list.Count > 0)
			{
				Net.SendStun(list, Plugin.BlindStun.Value);
			}
		}

		internal static void ApplyStun(IList<ulong> ids, float amount)
		{
			if (ids == null || ids.Count == 0)
			{
				return;
			}
			RoundManager instance = RoundManager.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			foreach (EnemyAI spawnedEnemy in instance.SpawnedEnemies)
			{
				if ((Object)(object)spawnedEnemy != (Object)null && !spawnedEnemy.isEnemyDead && ids.Contains(((NetworkBehaviour)spawnedEnemy).NetworkObjectId))
				{
					try
					{
						spawnedEnemy.SetEnemyStunned(true, amount, (PlayerControllerB)null);
					}
					catch (Exception ex)
					{
						Plugin.Log.LogWarning((object)("Ship Turret: couldn't stun " + ((Object)spawnedEnemy).name + ": " + ex.Message));
					}
				}
			}
		}
	}
	internal static class NetworkId
	{
		internal static ulong Local
		{
			get
			{
				if (!((Object)(object)Net.NM != (Object)null))
				{
					return 0uL;
				}
				return Net.NM.LocalClientId;
			}
		}
	}
	internal static class MenuHooks
	{
		internal const string ModMenuGuid = "daniel.lethalcompany.modmenu";

		private const string Tab = "Ship Turret";

		[MethodImpl(MethodImplOptions.NoInlining)]
		internal static void Register()
		{
			MenuApi.Button("Ship Turret", "Unlock the turret (free)", (Func<string>)Store.HostUnlockFree, true, true, (Func<string>)(() => (Store.Index >= 0) ? ((!Store.Unlocked) ? null : "already on the roof") : "join a lobby first"), (string)null);
			MenuApi.Button("Ship Turret", "Fire at the nearest outdoor monster", (Func<string>)Host.FireAtNearest, true, true, (Func<string>)(() => Turret.Built ? ((!((Object)(object)StartOfRound.Instance != (Object)null) || !StartOfRound.Instance.inShipPhase) ? null : "land first") : "unlock the turret first"), "turns the turret and fires, recharge or not");
			MenuApi.Button("Ship Turret", "Cool and recharge now", (Func<string>)Host.CoolNow, true, true, (Func<string>)(() => Turret.Built ? null : "unlock the turret first"), (string)null);
			MenuApi.Toggle("Ship Turret", "Spotlight", (Func<bool>)(() => Host.State.Light), (Func<bool, string>)delegate(bool on)
			{
				Host.SetLight(on);
				return "spotlight " + (on ? "on" : "off");
			}, true, false, (Func<string>)(() => Turret.Built ? null : "unlock the turret first"), (string)null);
			MenuApi.Button("Ship Turret", "Find the roof spot again", (Func<string>)delegate
			{
				if (!Turret.Built)
				{
					return "the turret isn't built";
				}
				Turret.Remove();
				Turret.Check();
				return (!Turret.Built) ? "couldn't rebuild it" : "turret moved";
			}, false, false, (Func<string>)(() => Store.Unlocked ? null : "unlock the turret first"), "this player's turret only");
			try
			{
				RegisterSettings();
			}
			catch (Exception ex)
			{
				Plugin.Log.LogInfo((object)("Ship Turret: settings rows need Mod Menu 0.3.6 or later (" + ex.GetType().Name + ")"));
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		private static void RegisterSettings()
		{
			MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.Price, "Price", true, "the store price, sent to every player", (Func<string>)null);
			MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.Blinds, "Spotlight dazzles", true, (string)null, (Func<string>)null);
			MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.MouseSpeed, "Mouse sensitivity", false, (string)null, (Func<string>)null);
			MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.KeySpeed, "Key turn speed", false, (string)null, (Func<string>)null);
			MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.InvertLook, "Invert look", false, (string)null, (Func<string>)null);
			MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.Volume, "Volume", false, "0 to 1", (Func<string>)null);
			MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.VerboseLog, "Verbose log", false, "testing: roof probe, shots and dazzles", (Func<string>)null);
			MenuApi.Tuning((Action)delegate
			{
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.ArcRange, "Arc range (m)", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.ArcStun, "Arc stun", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.ArcHeat, "Heat per shot", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.ArcRecharge, "Arc recharge (s)", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.ArcSplash, "Arc splash radius (m)", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.Cooling, "Cooling per second", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.LightRange, "Spotlight range (m)", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.BlindAfter, "Dazzle after (s)", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.BlindStun, "Dazzle stun", true, (string)null, (Func<string>)null);
				MenuApi.Setting("Ship Turret", (ConfigEntryBase)(object)Plugin.BlindCooldown, "Dazzle cooldown (s)", true, (string)null, (Func<string>)null);
			});
		}
	}
	internal static class Net
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static HandleNamedMessageDelegate <>9__18_0;

			public static HandleNamedMessageDelegate <>9__18_1;

			public static HandleNamedMessageDelegate <>9__18_2;

			public static HandleNamedMessageDelegate <>9__18_3;

			public static HandleNamedMessageDelegate <>9__18_4;

			public static HandleNamedMessageDelegate <>9__18_5;

			internal void <Register>b__18_0(ulong sender, FastBufferReader r)
			{
				if (IsHost)
				{
					SendState(sender);
				}
			}

			internal void <Register>b__18_1(ulong sender, FastBufferReader r)
			{
				//IL_000e: 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)
				//IL_0020: 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)
				if (IsHost)
				{
					float yaw = default(float);
					((FastBufferReader)(ref r)).ReadValueSafe<float>(ref yaw, default(ForPrimitives));
					float pitch = default(float);
					((FastBufferReader)(ref r)).ReadValueSafe<float>(ref pitch, default(ForPrimitives));
					Host.SetAim(sender, yaw, pitch);
				}
			}

			internal void <Register>b__18_2(ulong sender, FastBufferReader r)
			{
				//IL_000e: 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 (IsHost)
				{
					byte cmd = default(byte);
					((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref cmd, default(ForPrimitives));
					Host.Command(sender, cmd);
				}
			}

			internal void <Register>b__18_3(ulong sender, FastBufferReader r)
			{
				//IL_000e: 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)
				//IL_004c: 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_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_0076: 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_008d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0093: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a4: 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_00bb: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ce: 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_00f0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
				//IL_0116: 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)
				if (IsHost)
				{
					return;
				}
				byte b = default(byte);
				((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref b, default(ForPrimitives));
				if (b != 1)
				{
					Plugin.Log.LogWarning((object)$"Ship Turret: the host has another version of the mod (format {b}, ours {(byte)1}).");
					return;
				}
				TurretState turretState = new TurretState();
				int price = default(int);
				((FastBufferReader)(ref r)).ReadValueSafe<int>(ref price, default(ForPrimitives));
				float num = default(float);
				((FastBufferReader)(ref r)).ReadValueSafe<float>(ref num, default(ForPrimitives));
				((FastBufferReader)(ref r)).ReadValueSafe<float>(ref turretState.Yaw, default(ForPrimitives));
				((FastBufferReader)(ref r)).ReadValueSafe<float>(ref turretState.Pitch, default(ForPrimitives));
				((FastBufferReader)(ref r)).ReadValueSafe<bool>(ref turretState.Light, default(ForPrimitives));
				((FastBufferReader)(ref r)).ReadValueSafe<bool>(ref turretState.Overheated, default(ForPrimitives));
				byte b2 = default(byte);
				((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref b2, default(ForPrimitives));
				turretState.Heat = (float)(int)b2 / 2.55f;
				byte b3 = default(byte);
				((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref b3, default(ForPrimitives));
				turretState.Recharge = (float)(int)b3 / 255f;
				((FastBufferReader)(ref r)).ReadValueSafe<ulong>(ref turretState.Operator, default(ForPrimitives));
				Host.State = turretState;
				Store.ApplyPrice(price);
				if (!Mathf.Approximately(num, _lightRange))
				{
					_lightRange = num;
					Turret.ApplyBeam();
				}
			}

			internal void <Register>b__18_4(ulong sender, FastBufferReader r)
			{
				//IL_0017: 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_0023: 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 (!IsHost)
				{
					Vector3 end = default(Vector3);
					((FastBufferReader)(ref r)).ReadValueSafe(ref end);
					bool hit = default(bool);
					((FastBufferReader)(ref r)).ReadValueSafe<bool>(ref hit, default(ForPrimitives));
					Host.ApplyStun(ReadIds(r, out var amount), amount);
					Turret.Zap(end, hit);
				}
			}

			internal void <Register>b__18_5(ulong sender, FastBufferReader r)
			{
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				if (!IsHost)
				{
					Host.ApplyStun(ReadIds(r, out var amount), amount);
				}
			}
		}

		private const string Hello = "ShipTurret.Hello";

		private const string StateMsg = "ShipTurret.State";

		private const string Aim = "ShipTurret.Aim";

		private const string Cmd = "ShipTurret.Cmd";

		private const string ZapMsg = "ShipTurret.Zap";

		private const string StunMsg = "ShipTurret.Stun";

		private const byte Version = 1;

		private static CustomMessagingManager _registeredOn;

		private static bool _helloSent;

		private static float _lightRange = -1f;

		internal static NetworkManager NM => NetworkManager.Singleton;

		internal static bool Online
		{
			get
			{
				if ((Object)(object)NM != (Object)null && NM.IsListening)
				{
					return NM.CustomMessagingManager != null;
				}
				return false;
			}
		}

		internal static bool IsHost
		{
			get
			{
				if (Online)
				{
					return NM.IsServer;
				}
				return false;
			}
		}

		internal static float LightRange
		{
			get
			{
				if (IsHost || !(_lightRange > 0f))
				{
					return Plugin.LightRange.Value;
				}
				return _lightRange;
			}
		}

		internal static void Register()
		{
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Expected O, but got Unknown
			//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_00c9: Expected O, but got Unknown
			//IL_00e8: 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_00f3: Expected O, but got Unknown
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Expected O, but got Unknown
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Expected O, but got Unknown
			NetworkManager nM = NM;
			if ((Object)(object)nM == (Object)null || nM.CustomMessagingManager == null || _registeredOn == nM.CustomMessagingManager)
			{
				return;
			}
			_registeredOn = nM.CustomMessagingManager;
			_helloSent = false;
			_lightRange = -1f;
			Host.OnSessionStart();
			Turret.Remove();
			CustomMessagingManager customMessagingManager = nM.CustomMessagingManager;
			object obj = <>c.<>9__18_0;
			if (obj == null)
			{
				HandleNamedMessageDelegate val = delegate(ulong sender, FastBufferReader r)
				{
					if (IsHost)
					{
						SendState(sender);
					}
				};
				<>c.<>9__18_0 = val;
				obj = (object)val;
			}
			customMessagingManager.RegisterNamedMessageHandler("ShipTurret.Hello", (HandleNamedMessageDelegate)obj);
			object obj2 = <>c.<>9__18_1;
			if (obj2 == null)
			{
				HandleNamedMessageDelegate val2 = delegate(ulong sender, FastBufferReader r)
				{
					//IL_000e: 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)
					//IL_0020: 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)
					if (IsHost)
					{
						float yaw = default(float);
						((FastBufferReader)(ref r)).ReadValueSafe<float>(ref yaw, default(ForPrimitives));
						float pitch = default(float);
						((FastBufferReader)(ref r)).ReadValueSafe<float>(ref pitch, default(ForPrimitives));
						Host.SetAim(sender, yaw, pitch);
					}
				};
				<>c.<>9__18_1 = val2;
				obj2 = (object)val2;
			}
			customMessagingManager.RegisterNamedMessageHandler("ShipTurret.Aim", (HandleNamedMessageDelegate)obj2);
			object obj3 = <>c.<>9__18_2;
			if (obj3 == null)
			{
				HandleNamedMessageDelegate val3 = delegate(ulong sender, FastBufferReader r)
				{
					//IL_000e: 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 (IsHost)
					{
						byte cmd = default(byte);
						((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref cmd, default(ForPrimitives));
						Host.Command(sender, cmd);
					}
				};
				<>c.<>9__18_2 = val3;
				obj3 = (object)val3;
			}
			customMessagingManager.RegisterNamedMessageHandler("ShipTurret.Cmd", (HandleNamedMessageDelegate)obj3);
			object obj4 = <>c.<>9__18_3;
			if (obj4 == null)
			{
				HandleNamedMessageDelegate val4 = delegate(ulong sender, FastBufferReader r)
				{
					//IL_000e: 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)
					//IL_004c: 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_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_0076: 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_008d: Unknown result type (might be due to invalid IL or missing references)
					//IL_0093: Unknown result type (might be due to invalid IL or missing references)
					//IL_00a4: 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_00bb: Unknown result type (might be due to invalid IL or missing references)
					//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
					//IL_00ce: 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_00f0: Unknown result type (might be due to invalid IL or missing references)
					//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
					//IL_0116: 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)
					if (!IsHost)
					{
						byte b = default(byte);
						((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref b, default(ForPrimitives));
						if (b != 1)
						{
							Plugin.Log.LogWarning((object)$"Ship Turret: the host has another version of the mod (format {b}, ours {(byte)1}).");
						}
						else
						{
							TurretState turretState = new TurretState();
							int price = default(int);
							((FastBufferReader)(ref r)).ReadValueSafe<int>(ref price, default(ForPrimitives));
							float num = default(float);
							((FastBufferReader)(ref r)).ReadValueSafe<float>(ref num, default(ForPrimitives));
							((FastBufferReader)(ref r)).ReadValueSafe<float>(ref turretState.Yaw, default(ForPrimitives));
							((FastBufferReader)(ref r)).ReadValueSafe<float>(ref turretState.Pitch, default(ForPrimitives));
							((FastBufferReader)(ref r)).ReadValueSafe<bool>(ref turretState.Light, default(ForPrimitives));
							((FastBufferReader)(ref r)).ReadValueSafe<bool>(ref turretState.Overheated, default(ForPrimitives));
							byte b2 = default(byte);
							((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref b2, default(ForPrimitives));
							turretState.Heat = (float)(int)b2 / 2.55f;
							byte b3 = default(byte);
							((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref b3, default(ForPrimitives));
							turretState.Recharge = (float)(int)b3 / 255f;
							((FastBufferReader)(ref r)).ReadValueSafe<ulong>(ref turretState.Operator, default(ForPrimitives));
							Host.State = turretState;
							Store.ApplyPrice(price);
							if (!Mathf.Approximately(num, _lightRange))
							{
								_lightRange = num;
								Turret.ApplyBeam();
							}
						}
					}
				};
				<>c.<>9__18_3 = val4;
				obj4 = (object)val4;
			}
			customMessagingManager.RegisterNamedMessageHandler("ShipTurret.State", (HandleNamedMessageDelegate)obj4);
			object obj5 = <>c.<>9__18_4;
			if (obj5 == null)
			{
				HandleNamedMessageDelegate val5 = delegate(ulong sender, FastBufferReader r)
				{
					//IL_0017: 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_0023: 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 (!IsHost)
					{
						Vector3 end = default(Vector3);
						((FastBufferReader)(ref r)).ReadValueSafe(ref end);
						bool hit = default(bool);
						((FastBufferReader)(ref r)).ReadValueSafe<bool>(ref hit, default(ForPrimitives));
						Host.ApplyStun(ReadIds(r, out var amount), amount);
						Turret.Zap(end, hit);
					}
				};
				<>c.<>9__18_4 = val5;
				obj5 = (object)val5;
			}
			customMessagingManager.RegisterNamedMessageHandler("ShipTurret.Zap", (HandleNamedMessageDelegate)obj5);
			object obj6 = <>c.<>9__18_5;
			if (obj6 == null)
			{
				HandleNamedMessageDelegate val6 = delegate(ulong sender, FastBufferReader r)
				{
					//IL_0008: Unknown result type (might be due to invalid IL or missing references)
					if (!IsHost)
					{
						Host.ApplyStun(ReadIds(r, out var amount), amount);
					}
				};
				<>c.<>9__18_5 = val6;
				obj6 = (object)val6;
			}
			customMessagingManager.RegisterNamedMessageHandler("ShipTurret.Stun", (HandleNamedMessageDelegate)obj6);
		}

		internal unsafe static void SayHello()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			if (_helloSent || !Online || IsHost || !NM.IsConnectedClient)
			{
				return;
			}
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(8, (Allocator)2, -1);
			try
			{
				NM.CustomMessagingManager.SendNamedMessage("ShipTurret.Hello", 0uL, val, (NetworkDelivery)3);
				_helloSent = true;
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		internal unsafe static void SendAim(float yaw, float pitch)
		{
			//IL_0020: 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_0032: 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_004f: Unknown result type (might be due to invalid IL or missing references)
			if (!Online || IsHost)
			{
				return;
			}
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(16, (Allocator)2, -1);
			try
			{
				((FastBufferWriter)(ref val)).WriteValueSafe<float>(ref yaw, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<float>(ref pitch, default(ForPrimitives));
				NM.CustomMessagingManager.SendNamedMessage("ShipTurret.Aim", 0uL, val, (NetworkDelivery)1);
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		internal unsafe static void SendCommand(byte cmd)
		{
			//IL_001f: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			if (!Online || IsHost)
			{
				return;
			}
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(4, (Allocator)2, -1);
			try
			{
				((FastBufferWriter)(ref val)).WriteValueSafe<byte>(ref cmd, default(ForPrimitives));
				NM.CustomMessagingManager.SendNamedMessage("ShipTurret.Cmd", 0uL, val, (NetworkDelivery)3);
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		internal static void Broadcast()
		{
			if (!IsHost)
			{
				return;
			}
			foreach (ulong connectedClientsId in NM.ConnectedClientsIds)
			{
				if (connectedClientsId != 0L)
				{
					SendState(connectedClientsId);
				}
			}
		}

		private unsafe static void SendState(ulong cid)
		{
			//IL_0021: 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_003a: 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_0058: 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_006e: 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_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: 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)
			if (!IsHost)
			{
				return;
			}
			TurretState state = Host.State;
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(64, (Allocator)2, -1);
			try
			{
				byte b = 1;
				((FastBufferWriter)(ref val)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
				int currentPrice = Store.CurrentPrice;
				((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref currentPrice, default(ForPrimitives));
				float value = Plugin.LightRange.Value;
				((FastBufferWriter)(ref val)).WriteValueSafe<float>(ref value, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<float>(ref state.Yaw, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<float>(ref state.Pitch, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref state.Light, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref state.Overheated, default(ForPrimitives));
				b = (byte)Mathf.RoundToInt(state.Heat * 2.55f);
				((FastBufferWriter)(ref val)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
				b = (byte)Mathf.RoundToInt(state.Recharge * 255f);
				((FastBufferWriter)(ref val)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<ulong>(ref state.Operator, default(ForPrimitives));
				NM.CustomMessagingManager.SendNamedMessage("ShipTurret.State", cid, val, (NetworkDelivery)3);
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		internal unsafe static void SendZap(Vector3 end, bool hit, List<ulong> ids, float amount)
		{
			//IL_0007: 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_0045: 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)
			Host.ApplyStun(ids, amount);
			Turret.Zap(end, hit);
			if (!IsHost)
			{
				return;
			}
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(64 + ids.Count * 8, (Allocator)2, -1);
			try
			{
				((FastBufferWriter)(ref val)).WriteValueSafe(ref end);
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref hit, default(ForPrimitives));
				WriteIds(val, ids, amount);
				foreach (ulong connectedClientsId in NM.ConnectedClientsIds)
				{
					if (connectedClientsId != 0L)
					{
						NM.CustomMessagingManager.SendNamedMessage("ShipTurret.Zap", connectedClientsId, val, (NetworkDelivery)3);
					}
				}
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		internal unsafe static void SendStun(List<ulong> ids, float amount)
		{
			//IL_0023: 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)
			Host.ApplyStun(ids, amount);
			if (!IsHost)
			{
				return;
			}
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(16 + ids.Count * 8, (Allocator)2, -1);
			try
			{
				WriteIds(val, ids, amount);
				foreach (ulong connectedClientsId in NM.ConnectedClientsIds)
				{
					if (connectedClientsId != 0L)
					{
						NM.CustomMessagingManager.SendNamedMessage("ShipTurret.Stun", connectedClientsId, val, (NetworkDelivery)3);
					}
				}
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		private static void WriteIds(FastBufferWriter w, List<ulong> ids, float amount)
		{
			//IL_0006: 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_002a: 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_0048: 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)
			((FastBufferWriter)(ref w)).WriteValueSafe<float>(ref amount, default(ForPrimitives));
			byte b = (byte)Mathf.Min(ids.Count, 255);
			((FastBufferWriter)(ref w)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
			for (int i = 0; i < ids.Count && i < 255; i++)
			{
				ulong num = ids[i];
				((FastBufferWriter)(ref w)).WriteValueSafe<ulong>(ref num, default(ForPrimitives));
			}
		}

		private static List<ulong> ReadIds(FastBufferReader r, out float amount)
		{
			//IL_0005: 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_0017: 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_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			((FastBufferReader)(ref r)).ReadValueSafe<float>(ref amount, default(ForPrimitives));
			byte b = default(byte);
			((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref b, default(ForPrimitives));
			List<ulong> list = new List<ulong>(b);
			ulong item = default(ulong);
			for (int i = 0; i < b; i++)
			{
				((FastBufferReader)(ref r)).ReadValueSafe<ulong>(ref item, default(ForPrimitives));
				list.Add(item);
			}
			return list;
		}
	}
	public static class PluginInfo
	{
		public const string Guid = "daniel.lethalcompany.shipturret";

		public const string Name = "Ship Turret";

		public const string Version = "0.1.0";
	}
	[BepInPlugin("daniel.lethalcompany.shipturret", "Ship Turret", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<int> Price;

		internal static ConfigEntry<float> ArcRange;

		internal static ConfigEntry<float> ArcStun;

		internal static ConfigEntry<float> ArcHeat;

		internal static ConfigEntry<float> ArcRecharge;

		internal static ConfigEntry<float> ArcSplash;

		internal static ConfigEntry<float> Cooling;

		internal static ConfigEntry<bool> Blinds;

		internal static ConfigEntry<float> LightRange;

		internal static ConfigEntry<float> BlindAfter;

		internal static ConfigEntry<float> BlindStun;

		internal static ConfigEntry<float> BlindCooldown;

		internal static ConfigEntry<float> MouseSpeed;

		internal static ConfigEntry<float> KeySpeed;

		internal static ConfigEntry<float> Volume;

		internal static ConfigEntry<bool> InvertLook;

		internal static ConfigEntry<bool> VerboseLog;

		private readonly Harmony _harmony = new Harmony("daniel.lethalcompany.shipturret");

		private void Awake()
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Expected O, but got Unknown
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Expected O, but got Unknown
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Expected O, but got Unknown
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Expected O, but got Unknown
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Expected O, but got Unknown
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Expected O, but got Unknown
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_020a: Expected O, but got Unknown
			//IL_023d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0247: Expected O, but got Unknown
			//IL_027a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Expected O, but got Unknown
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c1: Expected O, but got Unknown
			//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fe: Expected O, but got Unknown
			//IL_0331: Unknown result type (might be due to invalid IL or missing references)
			//IL_033b: Expected O, but got Unknown
			//IL_038e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0398: Expected O, but got Unknown
			//IL_03c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cd: Expected O, but got Unknown
			//IL_03cd: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Price = ((BaseUnityPlugin)this).Config.Bind<int>("Store (host)", "Price", 350, new ConfigDescription("Ship upgrade price in credits.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 5000), Array.Empty<object>()));
			ArcRange = ((BaseUnityPlugin)this).Config.Bind<float>("Arc stunner (host)", "Range", 40f, new ConfigDescription("How far the arc reaches, in metres.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 120f), Array.Empty<object>()));
			ArcStun = ((BaseUnityPlugin)this).Config.Bind<float>("Arc stunner (host)", "Stun", 2.5f, new ConfigDescription("Stun given by a hit, in the game's stun units (each monster multiplies it by its own stun time; most hold still about twice this many seconds).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.2f, 10f), Array.Empty<object>()));
			ArcHeat = ((BaseUnityPlugin)this).Config.Bind<float>("Arc stunner (host)", "Heat per shot", 35f, new ConfigDescription("Heat each shot adds; at 100 the turret overheats and the arc locks until it has cooled right down.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			ArcRecharge = ((BaseUnityPlugin)this).Config.Bind<float>("Arc stunner (host)", "Recharge seconds", 3f, new ConfigDescription("Seconds before the arc can fire again.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.2f, 30f), Array.Empty<object>()));
			ArcSplash = ((BaseUnityPlugin)this).Config.Bind<float>("Arc stunner (host)", "Splash radius", 2.5f, new ConfigDescription("Other outdoor monsters this close to where the arc lands are stunned too (metres).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10f), Array.Empty<object>()));
			Cooling = ((BaseUnityPlugin)this).Config.Bind<float>("Arc stunner (host)", "Cooling per second", 6f, new ConfigDescription("Heat lost each second.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 50f), Array.Empty<object>()));
			Blinds = ((BaseUnityPlugin)this).Config.Bind<bool>("Spotlight (host)", "Spotlight dazzles", true, "Outdoor monsters held in the beam are dazzled for a moment.");
			LightRange = ((BaseUnityPlugin)this).Config.Bind<float>("Spotlight (host)", "Range", 45f, new ConfigDescription("How far the beam reaches and dazzles, in metres.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 120f), Array.Empty<object>()));
			BlindAfter = ((BaseUnityPlugin)this).Config.Bind<float>("Spotlight (host)", "Dazzle after seconds", 1f, new ConfigDescription("Seconds a monster must stay in the beam before it's dazzled.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), Array.Empty<object>()));
			BlindStun = ((BaseUnityPlugin)this).Config.Bind<float>("Spotlight (host)", "Dazzle stun", 0.8f, new ConfigDescription("Stun a dazzle gives, in the game's stun units.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 5f), Array.Empty<object>()));
			BlindCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("Spotlight (host)", "Dazzle cooldown", 8f, new ConfigDescription("Seconds before the same monster can be dazzled again.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 60f), Array.Empty<object>()));
			MouseSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Controls (client)", "Mouse sensitivity", 0.12f, new ConfigDescription("Degrees the turret turns per pixel of mouse movement.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 1f), Array.Empty<object>()));
			KeySpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Controls (client)", "Key turn speed", 70f, new ConfigDescription("Degrees a second the turret turns with WASD or the arrow keys.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(10f, 360f), Array.Empty<object>()));
			InvertLook = ((BaseUnityPlugin)this).Config.Bind<bool>("Controls (client)", "Invert look", false, "Mouse up tilts the turret down.");
			Volume = ((BaseUnityPlugin)this).Config.Bind<float>("Controls (client)", "Volume", 1f, new ConfigDescription("Volume of the turret's sounds (0 to 1).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			VerboseLog = ((BaseUnityPlugin)this).Config.Bind<bool>("Testing", "Verbose log", false, "Log the roof probe, shots and dazzles.");
			GameObject val = new GameObject("ShipTurret.Runner");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<Runner>();
			Art.Load();
			Store.Register();
			_harmony.PatchAll(typeof(Plugin).Assembly);
			DawnSaveFix.TryPatch(_harmony);
			Hooks.Init();
			((BaseUnityPlugin)this).Config.SettingChanged += delegate(object _, SettingChangedEventArgs a)
			{
				if ((object)a.ChangedSetting == LightRange)
				{
					Turret.ApplyBeam();
				}
				if (Net.IsHost && ((object)a.ChangedSetting == Price || (object)a.ChangedSetting == LightRange))
				{
					Store.ApplyPrice(Price.Value);
					Host.Dirty = true;
				}
			};
			if (Chainloader.PluginInfos.ContainsKey("daniel.lethalcompany.modmenu"))
			{
				MenuHooks.Register();
			}
			Log.LogInfo((object)"Ship Turret 0.1.0 loaded.");
		}

		internal static void Debug(string s)
		{
			if (VerboseLog != null && VerboseLog.Value)
			{
				Log.LogInfo((object)("Ship Turret: " + s));
			}
		}
	}
	internal class Runner : MonoBehaviour
	{
		internal static Runner Instance;

		private float _nextCheck;

		private void Awake()
		{
			Instance = this;
		}

		private void Update()
		{
			if ((Object)(object)StartOfRound.Instance == (Object)null)
			{
				if (View.Open)
				{
					View.Close();
				}
				return;
			}
			if (Time.unscaledTime >= _nextCheck)
			{
				_nextCheck = Time.unscaledTime + 1f;
				Net.Register();
				Net.SayHello();
				bool flag = Net.Online && Store.Unlocked;
				if (!flag && Turret.Built)
				{
					Turret.Remove();
				}
				else if (flag)
				{
					Turret.Check();
				}
			}
			if (Net.IsHost)
			{
				Host.Tick();
			}
			if (Turret.Built)
			{
				Turret.Tick();
			}
			View.Tick();
		}

		private void OnGUI()
		{
			View.Gui();
		}
	}
	internal static class Store
	{
		internal const string Name = "Ship turret";

		private static UnlockableItem _unlockable;

		private static TerminalNode _info;

		private static TerminalNode _operate;

		private static int _price = -1;

		private static TerminalNode _buy;

		private static TerminalNode _confirm;

		public static int Index
		{
			get
			{
				StartOfRound instance = StartOfRound.Instance;
				if ((Object)(object)instance == (Object)null || (Object)(object)instance.unlockablesList == (Object)null)
				{
					return -1;
				}
				return instance.unlockablesList.unlockables.FindIndex((UnlockableItem u) => u != null && u.unlockableName == "Ship turret");
			}
		}

		public static bool Unlocked
		{
			get
			{
				int index = Index;
				if (index >= 0)
				{
					return StartOfRound.Instance.unlockablesList.unlockables[index].hasBeenUnlockedByPlayer;
				}
				return false;
			}
		}

		internal static int CurrentPrice
		{
			get
			{
				if (_price < 0)
				{
					return Plugin.Price.Value;
				}
				return _price;
			}
		}

		public static void Register()
		{
			//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_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: 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_003a: 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_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			_unlockable = new UnlockableItem
			{
				unlockableName = "Ship turret",
				unlockableType = 1,
				prefabObject = null,
				spawnPrefab = false,
				IsPlaceable = false,
				alwaysInStock = true,
				canBeStored = false,
				maxNumber = 1,
				alreadyUnlocked = false,
				hasBeenUnlockedByPlayer = false
			};
			TerminalNode val = (_info = ScriptableObject.CreateInstance<TerminalNode>());
			((Object)val).name = "ShipTurretInfo";
			val.clearPreviousText = true;
			val.displayText = "SHIP TURRET\n\nA pan/tilt spotlight with an arc stunner, bolted to the ship's roof.\n\nType TURRET at this terminal to take the controls and look through its camera. Hold a monster in the spotlight to dazzle it, or fire the arc to stun it where it stands. The arc recharges slowly and the turret overheats if it's fired too often.\n\nIt only works on monsters outside.\n\n";
			_operate = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)_operate).name = "ShipTurretOperate";
			_operate.clearPreviousText = true;
			_operate.displayText = "Linking to the ship turret...\n\n";
			Unlockables.RegisterUnlockable(_unlockable, (StoreType)1, (TerminalNode)null, (TerminalNode)null, val, Plugin.Price.Value);
		}

		public static void ApplyPrice(int price)
		{
			price = Mathf.Max(0, price);
			if (price == _price)
			{
				return;
			}
			_price = price;
			if (_unlockable != null)
			{
				try
				{
					Unlockables.UpdateUnlockablePrice(_unlockable, price);
				}
				catch (Exception ex)
				{
					Plugin.Log.LogWarning((object)("Ship Turret: couldn't set the price: " + ex.Message));
				}
				if ((Object)(object)_buy != (Object)null)
				{
					_buy.itemCost = price;
				}
				if ((Object)(object)_confirm != (Object)null)
				{
					_confirm.itemCost = price;
				}
			}
		}

		public static string HostUnlockFree()
		{
			int index = Index;
			if (index < 0)
			{
				return null;
			}
			if (Unlocked)
			{
				return "the turret is already on the roof";
			}
			Terminal val = Object.FindObjectOfType<Terminal>();
			StartOfRound.Instance.BuyShipUnlockableServerRpc(index, ((Object)(object)val != (Object)null) ? val.groupCredits : 0);
			return "ship turret unlocked";
		}

		internal static TerminalNode Override(Terminal term, TerminalNode result)
		{
			if ((Object)(object)term == (Object)null || (Object)(object)term.screenText == (Object)null || (Object)(object)term.terminalNodes == (Object)null)
			{
				return result;
			}
			string text = term.screenText.text;
			int num = Mathf.Clamp(term.textAdded, 0, text.Length);
			string text2 = new string((from c in text.Substring(text.Length - num)
				where !char.IsPunctuation(c)
				select c).ToArray()).Trim().ToLowerInvariant();
			text2 = string.Join(" ", text2.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
			string text3 = null;
			string[] array = new string[3] { "turret", "ship turret", "shipturret" };
			foreach (string text4 in array)
			{
				if (text2 == text4)
				{
					text3 = (Unlocked ? "operate" : "buy");
				}
				else if (text2 == "buy " + text4)
				{
					text3 = "buy";
				}
				else if (text2 == "info " + text4)
				{
					text3 = "info";
				}
			}
			if (text3 == null)
			{
				return result;
			}
			if (text3 == "operate")
			{
				_operate.displayText = (Turret.Built ? "Linking to the ship turret...\n\n" : "The ship turret isn't answering. Try again in a moment.\n\n");
				if (Turret.Built)
				{
					View.OpenNextFrame(term);
				}
				return _operate;
			}
			int index = Index;
			if (index < 0)
			{
				Plugin.Log.LogWarning((object)"Ship Turret: terminal: not in the unlockables list yet.");
				return result;
			}
			TerminalNode val = ((text3 == "info") ? _info : BuyNode(index));
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Log.LogWarning((object)$"Ship Turret: terminal: no {text3} node for unlockable {index}.");
				return result;
			}
			return val;
		}

		private static TerminalNode BuyNode(int index)
		{
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Expected O, but got Unknown
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Expected O, but got Unknown
			int currentPrice = CurrentPrice;
			if ((Object)(object)_buy != (Object)null && _buy.shipUnlockableID == index)
			{
				_buy.itemCost = currentPrice;
				if ((Object)(object)_confirm != (Object)null)
				{
					_confirm.itemCost = currentPrice;
				}
				return _buy;
			}
			TerminalNode[] source = Resources.FindObjectsOfTypeAll<TerminalNode>();
			_buy = ((IEnumerable<TerminalNode>)source).FirstOrDefault((Func<TerminalNode, bool>)((TerminalNode n) => (Object)(object)n != (Object)null && n.shipUnlockableID == index && !n.buyUnlockable && n.isConfirmationNode && n.terminalOptions != null && n.terminalOptions.Length != 0));
			if ((Object)(object)_buy != (Object)null)
			{
				return _buy;
			}
			TerminalNode val = ((IEnumerable<TerminalNode>)source).FirstOrDefault((Func<TerminalNode, bool>)((TerminalNode n) => (Object)(object)n != (Object)null && n.shipUnlockableID >= 0 && n.shipUnlockableID != index && !n.buyUnlockable && n.isConfirmationNode && n.terminalOptions != null && n.terminalOptions.Any((CompatibleNoun o) => (Object)(object)o?.noun != (Object)null && o.noun.word == "confirm") && n.terminalOptions.Any((CompatibleNoun o) => (Object)(object)o?.noun != (Object)null && o.noun.word == "deny")));
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			CompatibleNoun val2 = val.terminalOptions.First((CompatibleNoun o) => o.noun.word == "confirm");
			CompatibleNoun val3 = val.terminalOptions.First((CompatibleNoun o) => o.noun.word == "deny");
			_confirm = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)_confirm).name = "ShipTurretBuyConfirm";
			_confirm.clearPreviousText = true;
			_confirm.displayText = "Ordered the Ship Turret! It's bolted to the roof. Your new balance is [playerCredits].\n\nType TURRET to take the controls.\n\n";
			_confirm.shipUnlockableID = index;
			_confirm.buyUnlockable = true;
			_confirm.itemCost = currentPrice;
			_confirm.playSyncedClip = (((Object)(object)val2.result != (Object)null) ? val2.result.playSyncedClip : (-1));
			_buy = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)_buy).name = "ShipTurretBuy";
			_buy.clearPreviousText = true;
			_buy.displayText = "You have requested to order the Ship Turret. Total cost of item: [totalCost].\n\nPlease CONFIRM or DENY.\n\n";
			_buy.shipUnlockableID = index;
			_buy.isConfirmationNode = true;
			_buy.overrideOptions = true;
			_buy.itemCost = currentPrice;
			_buy.terminalOptions = (CompatibleNoun[])(object)new CompatibleNoun[2]
			{
				new CompatibleNoun(val2.noun, _confirm),
				new CompatibleNoun(val3.noun, val3.result)
			};
			return _buy;
		}
	}
	[HarmonyPatch(typeof(Terminal), "ParsePlayerSentence")]
	internal static class TerminalParsePatch
	{
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		private static void Postfix(Terminal __instance, ref TerminalNode __result)
		{
			try
			{
				__result = Store.Override(__instance, __result);
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Ship Turret: terminal override: " + ex.Message));
			}
		}
	}
	internal static class DawnSaveFix
	{
		public static void TryPatch(Harmony harmony)
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			//IL_0063: Expected O, but got Unknown
			Type type = AccessTools.TypeByName("Dawn.Internal.UnlockableSaveDataHandler");
			MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, "SaveAllUnlockables", (Type[])null, (Type[])null) : null);
			if (!(methodInfo == null))
			{
				harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(DawnSaveFix), "Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(DawnSaveFix), "Finalizer", (Type[])null), (HarmonyMethod)null);
			}
		}

		private static void Prefix(out int __state)
		{
			__state = -1;
			StartOfRound instance = StartOfRound.Instance;
			int index = Store.Index;
			if (!((Object)(object)instance == (Object)null) && index >= 0 && !instance.SpawnedShipUnlockables.ContainsKey(index))
			{
				instance.SpawnedShipUnlockables[index] = (((Object)(object)Turret.Root != (Object)null) ? Turret.Root : ((Component)instance).gameObject);
				__state = index;
			}
		}

		private static Exception Finalizer(Exception __exception, int __state)
		{
			if (__state >= 0)
			{
				StartOfRound.Instance?.SpawnedShipUnlockables.Remove(__state);
			}
			return __exception;
		}
	}
	internal static class Turret
	{
		internal static GameObject Root;

		private static Transform _yaw;

		private static Transform _head;

		private static Light _beam;

		private static Material _lensMat;

		private static Material _coilMat;

		private static AudioSource _audio;

		private static GameObject _hullAt;

		private static bool _hullKnown;

		private static float _yawNow;

		private static float _pitchNow;

		private static float _beamCandela;

		private static bool _lightShown;

		private static ulong _operatorShown = ulong.MaxValue;

		private static Material _arcMat;

		private static readonly Color LensOff = new Color(0.1f, 0.11f, 0.12f);

		private static readonly Color LensOn = new Color(1f, 0.93f, 0.78f);

		private static readonly Color CoilReady = new Color(0.35f, 0.75f, 1f);

		private static readonly Color CoilCold = new Color(0.12f, 0.1f, 0.08f);

		private static readonly Color CoilHot = new Color(1f, 0.28f, 0.06f);

		internal static bool Built => (Object)(object)Root != (Object)null;

		private static Vector3 HeadPivot => Root.transform.TransformPoint(new Vector3(0f, 0.82f, 0f));

		internal static Transform Head => _head;

		internal static void Check()
		{
			GameObject hull = Hooks.Hull;
			if (!Built || !_hullKnown || !((Object)(object)hull == (Object)(object)_hullAt))
			{
				if (!Built)
				{
					Build();
				}
				if (Built)
				{
					Place(hull);
				}
			}
		}

		internal static void Remove()
		{
			if (View.Open)
			{
				View.Close();
			}
			if ((Object)(object)Root != (Object)null)
			{
				Object.Destroy((Object)(object)Root);
			}
			Root = null;
			_beam = null;
			_audio = null;
			_hullKnown = false;
		}

		private static void Build()
		{
			//IL_004f: 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_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Expected O, but got Unknown
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: 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_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: 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)
			StartOfRound instance = StartOfRound.Instance;
			if (!((Object)(object)instance == (Object)null) && !((Object)(object)instance.elevatorTransform == (Object)null) && Art.Meshes.ContainsKey("Base"))
			{
				Sounds.Find();
				Material mat = Mat(LitSource(instance), "Body", (Texture)(object)Art.Palette);
				_lensMat = UnlitMat("Lens", LensOff);
				_coilMat = UnlitMat("Coil", CoilReady);
				Root = new GameObject("ShipTurret");
				Root.transform.SetParent(instance.elevatorTransform, false);
				Root.transform.localScale = Vector3.one * 1.3f;
				Part("Base", Root.transform, Vector3.zero, mat);
				_yaw = Part("Yaw", Root.transform, new Vector3(0f, 0.4f, 0f), mat);
				_head = Part("Head", _yaw, new Vector3(0f, 0.42f, 0f), mat);
				Part("Lens", _head, Vector3.zero, _lensMat);
				Part("Coil", _head, Vector3.zero, _coilMat);
				FlashlightItem val = Flashlights.Pro();
				if ((Object)(object)val != (Object)null && (Object)(object)val.flashlightBulb != (Object)null)
				{
					GameObject obj = Object.Instantiate<GameObject>(((Component)val.flashlightBulb).gameObject, _head, false);
					((Object)obj).name = "Spotlight";
					obj.transform.localPosition = Art.LensFront + new Vector3(0f, 0f, 0.03f);
					obj.transform.localRotation = Quaternion.identity;
					obj.transform.localScale = Vector3.one / 1.3f;
					_beam = obj.GetComponent<Light>();
					_beamCandela = val.flashlightBulb.intensity;
					ApplyBeam();
					((Behaviour)_beam).enabled = false;
				}
				else
				{
					Plugin.Log.LogWarning((object)"Ship Turret: no flashlight to copy the spotlight from; the turret has no beam.");
				}
				_audio = ((Component)_head).gameObject.AddComponent<AudioSource>();
				_audio.spatialBlend = 1f;
				_audio.rolloffMode = (AudioRolloffMode)1;
				_audio.minDistance = 4f;
				_audio.maxDistance = 45f;
				_audio.playOnAwake = false;
				_audio.dopplerLevel = 0f;
				_yawNow = Host.State.Yaw;
				_pitchNow = Host.State.Pitch;
				_lightShown = false;
				_operatorShown = ulong.MaxValue;
				Plugin.Debug("built");
			}
		}

		private static Transform Part(string mesh, Transform parent, Vector3 at, Material mat)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject(mesh);
			val.transform.SetParent(parent, false);
			val.transform.localPosition = at;
			if (!Art.Meshes.TryGetValue(mesh, out var value))
			{
				return val.transform;
			}
			val.AddComponent<MeshFilter>().sharedMesh = value;
			MeshRenderer obj = val.AddComponent<MeshRenderer>();
			((Renderer)obj).sharedMaterial = mat;
			((Renderer)obj).shadowCastingMode = (ShadowCastingMode)1;
			return val.transform;
		}

		internal static void ApplyBeam()
		{
			//IL_00a8: 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)
			if (!((Object)(object)_beam == (Object)null))
			{
				float lightRange = Net.LightRange;
				HDAdditionalLightData component = ((Component)_beam).GetComponent<HDAdditionalLightData>();
				if ((Object)(object)component != (Object)null)
				{
					component.spotLightShape = (SpotLightShape)0;
					component.SetSpotAngle(36f, 40f);
					component.lightUnit = (LightUnit)1;
					component.intensity = _beamCandela * 6f;
					component.range = lightRange;
					component.color = LensOn;
					component.volumetricDimmer = 0.6f;
				}
				_beam.spotAngle = 36f;
				_beam.range = lightRange;
				_beam.intensity = _beamCandela * 6f;
				_beam.color = LensOn;
			}
		}

		private static void Place(GameObject hull)
		{
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: 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)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_041f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0430: Unknown result type (might be due to invalid IL or missing references)
			//IL_0445: Unknown result type (might be due to invalid IL or missing references)
			//IL_0447: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fa: 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_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: 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_0210: Unknown result type (might be due to invalid IL or missing references)
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			//IL_0274: Unknown result type (might be due to invalid IL or missing references)
			//IL_0279: Unknown result type (might be due to invalid IL or missing references)
			//IL_0280: Unknown result type (might be due to invalid IL or missing references)
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_028c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0291: Unknown result type (might be due to invalid IL or missing references)
			//IL_0298: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cd: 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_02d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02db: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0305: Unknown result type (might be due to invalid IL or missing references)
			//IL_0311: Unknown result type (might be due to invalid IL or missing references)
			//IL_0344: Unknown result type (might be due to invalid IL or missing references)
			//IL_0349: Unknown result type (might be due to invalid IL or missing references)
			//IL_0353: Unknown result type (might be due to invalid IL or missing references)
			//IL_0358: Unknown result type (might be due to invalid IL or missing references)
			//IL_035d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0370: Unknown result type (might be due to invalid IL or missing references)
			//IL_0382: Unknown result type (might be due to invalid IL or missing references)
			//IL_038c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0394: Unknown result type (might be due to invalid IL or missing references)
			//IL_0399: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bc: Unknown result type (might be due to invalid IL or missing references)
			StartOfRound instance = StartOfRound.Instance;
			Transform ship = instance.elevatorTransform;
			_hullAt = hull;
			_hullKnown = true;
			List<MeshRenderer> list = (from r in ((Component)(((Object)(object)hull != (Object)null) ? hull.transform : ship)).GetComponentsInChildren<MeshRenderer>(false)
				where ((Renderer)r).enabled && !((Component)r).transform.IsChildOf(Root.transform) && (Object)(object)((Component)r).GetComponentInParent<GrabbableObject>() == (Object)null
				select r).ToList();
			if (list.Count == 0)
			{
				Root.transform.localPosition = Vector3.up * 4f;
				return;
			}
			Bounds bounds = ((Renderer)list[0]).bounds;
			foreach (MeshRenderer item in list)
			{
				((Bounds)(ref bounds)).Encapsulate(((Renderer)item).bounds);
			}
			Vector3 center = ((Bounds)(ref bounds)).center;
			Vector3 val = Vector3.zero;
			float num = float.MinValue;
			float num2 = 0.57f;
			float num3 = 1.3649999f;
			int collidersAndRoomMaskAndDefault = instance.collidersAndRoomMaskAndDefault;
			RaycastHit val2 = default(RaycastHit);
			RaycastHit val4 = default(RaycastHit);
			for (int num4 = 0; num4 < 25; num4++)
			{
				for (int num5 = 0; num5 < 13; num5++)
				{
					float num6 = Mathf.Lerp(((Bounds)(ref bounds)).min.x, ((Bounds)(ref bounds)).max.x, ((float)num4 + 0.5f) / 25f);
					float num7 = Mathf.Lerp(((Bounds)(ref bounds)).min.z, ((Bounds)(ref bounds)).max.z, ((float)num5 + 0.5f) / 13f);
					if (((Bounds)(ref bounds)).size.z > ((Bounds)(ref bounds)).size.x)
					{
						num6 = Mathf.Lerp(((Bounds)(ref bounds)).min.x, ((Bounds)(ref bounds)).max.x, ((float)num5 + 0.5f) / 13f);
						num7 = Mathf.Lerp(((Bounds)(ref bounds)).min.z, ((Bounds)(ref bounds)).max.z, ((float)num4 + 0.5f) / 25f);
					}
					if (!Physics.Raycast(new Vector3(num6, ((Bounds)(ref bounds)).max.y + 2f, num7), Vector3.down, ref val2, ((Bounds)(ref bounds)).size.y + 4f, collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1) || !Mine(((Component)((RaycastHit)(ref val2)).collider).transform) || ((RaycastHit)(ref val2)).normal.y < 0.9f)
					{
						continue;
					}
					bool flag = true;
					Vector3[] array = (Vector3[])(object)new Vector3[4]
					{
						Vector3.right,
						Vector3.left,
						Vector3.forward,
						Vector3.back
					};
					foreach (Vector3 val3 in array)
					{
						if (!Physics.Raycast(new Vector3(num6, ((Bounds)(ref bounds)).max.y + 2f, num7) + val3 * num2, Vector3.down, ref val4, ((Bounds)(ref bounds)).size.y + 4f, collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1) || Mathf.Abs(((RaycastHit)(ref val4)).point.y - ((RaycastHit)(ref val2)).point.y) > 0.06f)
						{
							flag = false;
							break;
						}
					}
					if (flag && !Physics.Raycast(((RaycastHit)(ref val2)).point + Vector3.up * 0.05f, Vector3.up, num3, collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1))
					{
						float num9 = ((RaycastHit)(ref val2)).point.y * 2f;
						Vector2 val5 = new Vector2(num6 - center.x, num7 - center.z);
						float num10 = num9 - ((Vector2)(ref val5)).magnitude * 0.3f;
						if (num10 > num)
						{
							num = num10;
							val = ((RaycastHit)(ref val2)).point;
						}
					}
				}
			}
			if (num == float.MinValue)
			{
				((Vector3)(ref val))..ctor(center.x, ((Bounds)(ref bounds)).max.y, center.z);
				Plugin.Log.LogWarning((object)"Ship Turret: found no flat roof with room; the turret sits on top of the ship's bounds.");
			}
			Root.transform.position = val;
			Root.transform.localRotation = Quaternion.identity;
			Plugin.Debug(string.Format("roof spot {0} (ship local), hull {1}", ship.InverseTransformPoint(val), ((Object)(object)hull != (Object)null) ? ((Object)hull).name : "vanilla"));
			bool Mine(Transform t)
			{
				if (!t.IsChildOf(ship))
				{
					if ((Object)(object)hull != (Object)null)
					{
						return t.IsChildOf(hull.transform);
					}
					return false;
				}
				return true;
			}
		}

		private static Quaternion HeadRot(float yaw, float pitch)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: 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_0030: 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)
			return Root.transform.rotation * Quaternion.Euler(0f, yaw, 0f) * Quaternion.Euler(0f - pitch, 0f, 0f);
		}

		internal static void Ray(float yaw, float pitch, out Vector3 muzzle, out Vector3 dir)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: 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_002e: 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_0039: Unknown result type (might be due to invalid IL or missing references)
			Quaternion val = HeadRot(yaw, pitch);
			muzzle = HeadPivot + val * (Art.Muzzle * 1.3f);
			dir = val * Vector3.forward;
		}

		internal static Vector3 LensWorld(float yaw, float pitch)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: 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_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			return HeadPivot + HeadRot(yaw, pitch) * (Art.LensFront * 1.3f);
		}

		internal static void AimAt(Vector3 target, out float yaw, out float pitch)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: 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_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: 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_004f: 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_005b: 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)
			Vector3 val = Quaternion.Inverse(Root.transform.rotation) * (target - HeadPivot);
			yaw = Mathf.Repeat(Mathf.Atan2(val.x, val.z) * 57.29578f, 360f);
			float y = val.y;
			Vector2 val2 = new Vector2(val.x, val.z);
			pitch = Mathf.Clamp(Mathf.Atan2(y, ((Vector2)(ref val2)).magnitude) * 57.29578f, -30f, 55f);
		}

		internal static void Tick()
		{
			//IL_008f: 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_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: 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_01fd: 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_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_0210: Unknown result type (might be due to invalid IL or missing references)
			TurretState state = Host.State;
			float yaw = state.Yaw;
			float pitch = state.Pitch;
			if (View.Open)
			{
				yaw = View.Yaw;
				pitch = View.Pitch;
			}
			float num = 1f - Mathf.Exp(-14f * Time.deltaTime);
			_yawNow = (View.Open ? yaw : (_yawNow + Mathf.DeltaAngle(_yawNow, yaw) * num));
			_pitchNow = (View.Open ? pitch : Mathf.Lerp(_pitchNow, pitch, num));
			_yaw.localRotation = Quaternion.Euler(0f, _yawNow, 0f);
			_head.localRotation = Quaternion.Euler(0f - _pitchNow, 0f, 0f);
			if (state.Light != _lightShown)
			{
				_lightShown = state.Light;
				if ((Object)(object)_beam != (Object)null)
				{
					((Behaviour)_beam).enabled = state.Light;
				}
				Play(Sounds.Click, 0.8f);
			}
			if (state.Operator != _operatorShown)
			{
				if (state.Operator != ulong.MaxValue)
				{
					Play(Sounds.Wake, 0.6f);
				}
				else if (_operatorShown != ulong.MaxValue)
				{
					Play(Sounds.Sleep, 0.6f);
				}
				_operatorShown = state.Operator;
			}
			if ((Object)(object)_lensMat != (Object)null)
			{
				SetUnlit(_lensMat, state.Light ? (LensOn * 1.6f) : LensOff);
			}
			if ((Object)(object)_coilMat != (Object)null)
			{
				float num2 = state.Heat / 100f;
				Color val = (state.Overheated ? Color.Lerp(CoilCold, CoilHot, 0.6f + 0.4f * Mathf.PingPong(Time.time * 3f, 1f)) : Color.Lerp(Color.Lerp(CoilCold, CoilReady, state.Recharge * state.Recharge), CoilHot, num2 * 0.8f));
				SetUnlit(_coilMat, val * 1.4f);
			}
		}

		internal static void Play(AudioClip clip, float vol)
		{
			if (!((Object)(object)clip == (Object)null) && !((Object)(object)_audio == (Object)null))
			{
				_audio.PlayOneShot(clip, vol * Plugin.Volume.Value);
			}
		}

		internal static void Zap(Vector3 end, bool hit)
		{
			//IL_002f: 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)
			if (Built)
			{
				Ray(_yawNow, _pitchNow, out var muzzle, out var _);
				Play(Sounds.Zap, 1f);
				((MonoBehaviour)Runner.Instance).StartCoroutine(Arc(muzzle, end, hit));
			}
		}

		private static IEnumerator Arc(Vector3 a, Vector3 b, bool hit)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			GameObject root = new GameObject("ShipTurret.Arc");
			List<LineRenderer> lines = new List<LineRenderer>();
			for (int i = 0; i < 3; i++)
			{
				GameObject val = new GameObject("Bolt" + i);
				val.transform.SetParent(root.transform, false);
				LineRenderer val2 = val.AddComponent<LineRenderer>();
				((Renderer)val2).sharedMaterial = ArcMaterial((i == 0) ? new Color(0.85f, 0.95f, 1f) : new Color(0.35f, 0.65f, 1f));
				val2.widthMultiplier = ((i == 0) ? 0.07f : 0.035f);
				val2.positionCount = 18;
				val2.useWorldSpace = true;
				((Renderer)val2).shadowCastingMode = (ShadowCastingMode)0;
				((Renderer)val2).receiveShadows = false;
				lines.Add(val2);
			}
			GameObject val3 = new GameObject("Flash");
			val3.transform.SetParent(root.transform, false);
			val3.transform.position = b;
			val3.AddComponent<Light>().type = (LightType)2;
			HDAdditionalLightData fhd = val3.AddComponent<HDAdditionalLightData>();
			fhd.lightUnit = (LightUnit)0;
			fhd.intensity = (hit ? 9000f : 4000f);
			fhd.range = 9f;
			fhd.color = new Color(0.55f, 0.75f, 1f);
			float t = 0f;
			float life = 0.32f;
			float nextJitter = 0f;
			while (t < life && (Object)(object)root != (Object)null)
			{
				if (t >= nextJitter)
				{
					nextJitter = t + 0.035f;
					for (int j = 0; j < lines.Count; j++)
					{
						Jag(lines[j], a, b, (j == 0) ? 0.35f : 0.6f);
					}
				}
				fl