Decompiled source of Shifter v1.0.0

Shifter.dll

Decompiled 3 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using DunGen;
using DunGen.Graph;
using DunGen.Tags;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Shifter;

internal class DungeonState
{
	internal class Seal
	{
		public readonly List<GameObject> Spawned = new List<GameObject>();

		public readonly List<GameObject> Hidden = new List<GameObject>();
	}

	public readonly Dungeon Dungeon;

	public readonly List<DoorwayConnection> Connections = new List<DoorwayConnection>();

	public readonly HashSet<Tile> CaveTiles = new HashSet<Tile>();

	public readonly List<Tile> AllTiles = new List<Tile>();

	public readonly Dictionary<long, int> Adjacency = new Dictionary<long, int>();

	public readonly Dictionary<Tile, List<int>> TileConns = new Dictionary<Tile, List<int>>();

	public readonly HashSet<int> Sealable = new HashSet<int>();

	public readonly List<int> CaveExits = new List<int>();

	public readonly List<int> CaveInternal = new List<int>();

	public readonly Dictionary<int, Seal> Sealed = new Dictionary<int, Seal>();

	private readonly Dictionary<Tile, int> tileIds = new Dictionary<Tile, int>();

	private static readonly FieldInfo SpawnedBlockerField = typeof(Doorway).GetField("spawnedBlockerPrefab", BindingFlags.Instance | BindingFlags.NonPublic);

	private static readonly Color Tint = new Color(1f, 0.15f, 0.12f, 1f);

	public DungeonState(Dungeon dungeon)
	{
		Dungeon = dungeon;
		foreach (Tile allTile in dungeon.AllTiles)
		{
			if (!((Object)(object)allTile == (Object)null))
			{
				tileIds[allTile] = AllTiles.Count;
				AllTiles.Add(allTile);
				if (IsCaveTile(allTile))
				{
					CaveTiles.Add(allTile);
				}
			}
		}
		int num = 0;
		foreach (DoorwayConnection connection in dungeon.Connections)
		{
			Connections.Add(connection);
			if (connection != null && (Object)(object)connection.A != (Object)null && (Object)(object)connection.B != (Object)null && (Object)(object)connection.A.Tile != (Object)null && (Object)(object)connection.B.Tile != (Object)null)
			{
				Adjacency[Key(connection.A.Tile, connection.B.Tile)] = num;
				AddTileConn(connection.A.Tile, num);
				AddTileConn(connection.B.Tile, num);
				bool flag = CaveTiles.Contains(connection.A.Tile);
				bool flag2 = CaveTiles.Contains(connection.B.Tile);
				if ((flag || flag2) && CanSeal(num))
				{
					Sealable.Add(num);
					if (flag && flag2)
					{
						CaveInternal.Add(num);
					}
					else
					{
						CaveExits.Add(num);
					}
				}
			}
			num++;
		}
		Plugin.Log.LogInfo((object)$"Interior scanned: {AllTiles.Count} tiles, {CaveTiles.Count} cave tiles, {Connections.Count} connections, {Sealable.Count} cave openings that can shift ({CaveExits.Count} lead out of the caves).");
		if (!Plugin.VerboseLogging.Value)
		{
			return;
		}
		HashSet<string> hashSet = new HashSet<string>();
		foreach (Tile allTile2 in AllTiles)
		{
			if (allTile2.Tags == null || allTile2.Tags.Tags == null)
			{
				continue;
			}
			foreach (Tag tag in allTile2.Tags.Tags)
			{
				hashSet.Add(SafeTagName(tag));
			}
		}
		Plugin.Log.LogInfo((object)("Tile tags in this interior: " + string.Join(", ", new List<string>(hashSet).ToArray())));
	}

	private void AddTileConn(Tile t, int i)
	{
		if (!TileConns.TryGetValue(t, out var value))
		{
			value = new List<int>();
			TileConns[t] = value;
		}
		value.Add(i);
	}

	public Tile Other(int index, Tile from)
	{
		DoorwayConnection val = Connections[index];
		return (!((Object)(object)val.A.Tile == (Object)(object)from)) ? val.A.Tile : val.B.Tile;
	}

	public bool AllSealed(Tile t, int extra)
	{
		if ((Object)(object)t == (Object)null || !TileConns.TryGetValue(t, out var value) || value.Count == 0)
		{
			return false;
		}
		foreach (int item in value)
		{
			if (item != extra && !Sealed.ContainsKey(item))
			{
				return false;
			}
		}
		return true;
	}

	public Dictionary<Tile, int> TileDistances(Tile from)
	{
		Dictionary<Tile, int> dictionary = new Dictionary<Tile, int>();
		if ((Object)(object)from == (Object)null)
		{
			return dictionary;
		}
		Queue<Tile> queue = new Queue<Tile>();
		dictionary[from] = 0;
		queue.Enqueue(from);
		while (queue.Count > 0)
		{
			Tile val = queue.Dequeue();
			if (!TileConns.TryGetValue(val, out var value))
			{
				continue;
			}
			foreach (int item in value)
			{
				Tile val2 = Other(item, val);
				if ((Object)(object)val2 != (Object)null && !dictionary.ContainsKey(val2))
				{
					dictionary[val2] = dictionary[val] + 1;
					queue.Enqueue(val2);
				}
			}
		}
		return dictionary;
	}

	public int TileDistance(Tile a, Tile b)
	{
		int value;
		return (!TileDistances(a).TryGetValue(b, out value)) ? (-1) : value;
	}

	public List<int> RouteTo(Tile from, HashSet<Tile> targets, int maxRooms)
	{
		if ((Object)(object)from == (Object)null || targets.Count == 0)
		{
			return null;
		}
		if (targets.Contains(from))
		{
			return new List<int>();
		}
		Dictionary<Tile, int> dictionary = new Dictionary<Tile, int>();
		Dictionary<Tile, Tile> dictionary2 = new Dictionary<Tile, Tile>();
		Dictionary<Tile, int> dictionary3 = new Dictionary<Tile, int>();
		Queue<Tile> queue = new Queue<Tile>();
		dictionary3[from] = 0;
		queue.Enqueue(from);
		while (queue.Count > 0)
		{
			Tile val = queue.Dequeue();
			if (dictionary3[val] >= maxRooms || !TileConns.TryGetValue(val, out var value))
			{
				continue;
			}
			foreach (int item in value)
			{
				if (Sealed.ContainsKey(item))
				{
					continue;
				}
				Tile val2 = Other(item, val);
				if ((Object)(object)val2 == (Object)null || dictionary3.ContainsKey(val2))
				{
					continue;
				}
				dictionary3[val2] = dictionary3[val] + 1;
				dictionary[val2] = item;
				dictionary2[val2] = val;
				if (targets.Contains(val2))
				{
					List<int> list = new List<int>();
					Tile val3 = val2;
					while ((Object)(object)val3 != (Object)(object)from)
					{
						list.Add(dictionary[val3]);
						val3 = dictionary2[val3];
					}
					list.Reverse();
					return list;
				}
				queue.Enqueue(val2);
			}
		}
		return null;
	}

	private static string SafeTagName(Tag tag)
	{
		try
		{
			return (!(tag != (Tag)null)) ? string.Empty : (tag.Name ?? string.Empty);
		}
		catch
		{
			return string.Empty;
		}
	}

	private static bool IsCaveTile(Tile t)
	{
		if (t.Tags != null && t.Tags.Tags != null)
		{
			foreach (Tag tag in t.Tags.Tags)
			{
				if (SafeTagName(tag).IndexOf("cave", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return true;
				}
			}
		}
		return ((Object)((Component)t).gameObject).name.IndexOf("cave", StringComparison.OrdinalIgnoreCase) >= 0;
	}

	public bool IsCave(Tile t)
	{
		return (Object)(object)t != (Object)null && CaveTiles.Contains(t);
	}

	private long Key(Tile a, Tile b)
	{
		long num = tileIds[a];
		long num2 = tileIds[b];
		if (num > num2)
		{
			long num3 = num;
			num = num2;
			num2 = num3;
		}
		return (num << 32) | num2;
	}

	public bool TryGetConnection(Tile a, Tile b, out int index)
	{
		index = -1;
		if ((Object)(object)a == (Object)null || (Object)(object)b == (Object)null || !tileIds.ContainsKey(a) || !tileIds.ContainsKey(b))
		{
			return false;
		}
		return Adjacency.TryGetValue(Key(a, b), out index);
	}

	public Tile FindTile(Vector3 pos, Tile hint)
	{
		//IL_0001: 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_002b: 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_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: 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_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_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)
		Tile val = FloorTile(pos);
		if ((Object)(object)val != (Object)null)
		{
			return val;
		}
		if ((Object)(object)hint != (Object)null)
		{
			Bounds bounds = hint.Bounds;
			if (((Bounds)(ref bounds)).Contains(pos))
			{
				return hint;
			}
		}
		Tile result = null;
		float num = float.MaxValue;
		foreach (Tile allTile in AllTiles)
		{
			if ((Object)(object)allTile == (Object)null)
			{
				continue;
			}
			Bounds bounds2 = allTile.Bounds;
			if (((Bounds)(ref bounds2)).Contains(pos))
			{
				float num2 = ((Bounds)(ref bounds2)).size.x * ((Bounds)(ref bounds2)).size.y * ((Bounds)(ref bounds2)).size.z;
				if (num2 < num)
				{
					num = num2;
					result = allTile;
				}
			}
		}
		return result;
	}

	public Tile FloorTile(Vector3 pos)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		int num = ((!((Object)(object)StartOfRound.Instance != (Object)null)) ? (-1) : StartOfRound.Instance.collidersAndRoomMaskAndDefault);
		RaycastHit val = default(RaycastHit);
		if (!Physics.Raycast(pos + Vector3.up * 0.6f, Vector3.down, ref val, 4f, num, (QueryTriggerInteraction)1))
		{
			return null;
		}
		Tile componentInParent = ((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Tile>();
		return (!((Object)(object)componentInParent != (Object)null) || !tileIds.ContainsKey(componentInParent)) ? null : componentInParent;
	}

	public static Vector2 SocketSize(Doorway d)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if ((Object)(object)d.Socket != (Object)null)
			{
				return d.Socket.Size;
			}
		}
		catch
		{
		}
		return new Vector2(3f, 3f);
	}

	public Vector3 OpeningCentre(int index)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: 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_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		Doorway a = Connections[index].A;
		Vector2 val = SocketSize(a);
		return ((Component)a).transform.position + ((Component)a).transform.up * (val.y * 0.5f);
	}

	private bool CanSeal(int index)
	{
		DoorwayConnection val = Connections[index];
		if (val.A.HasDoorPrefabInstance || val.B.HasDoorPrefabInstance)
		{
			return false;
		}
		return HasVisualSource(val.A) || HasVisualSource(val.B);
	}

	private bool HasVisualSource(Doorway d)
	{
		if ((Object)(object)FirstViable(d.BlockerPrefabWeights) != (Object)null)
		{
			return true;
		}
		return (Object)(object)FindDonor(d) != (Object)null;
	}

	private static GameObject FirstViable(List<GameObjectWeight> list)
	{
		if (list == null)
		{
			return null;
		}
		foreach (GameObjectWeight item in list)
		{
			if (item != null && (Object)(object)item.GameObject != (Object)null && item.Weight > 0f)
			{
				return item.GameObject;
			}
		}
		return null;
	}

	private static GameObject PickWeighted(List<GameObjectWeight> list, Random rng)
	{
		float num = 0f;
		foreach (GameObjectWeight item in list)
		{
			if (item != null && (Object)(object)item.GameObject != (Object)null && item.Weight > 0f)
			{
				num += item.Weight;
			}
		}
		if (num <= 0f)
		{
			return null;
		}
		float num2 = (float)rng.NextDouble() * num;
		foreach (GameObjectWeight item2 in list)
		{
			if (item2 != null && !((Object)(object)item2.GameObject == (Object)null) && !(item2.Weight <= 0f))
			{
				num2 -= item2.Weight;
				if (num2 <= 0f)
				{
					return item2.GameObject;
				}
			}
		}
		return FirstViable(list);
	}

	private Doorway FindDonor(Doorway d)
	{
		Tile tile = d.Tile;
		if ((Object)(object)tile == (Object)null)
		{
			return null;
		}
		int num = tile.AllDoorways.IndexOf(d);
		GameObject val = null;
		try
		{
			val = tile.Prefab;
		}
		catch
		{
		}
		if ((Object)(object)val != (Object)null && num >= 0)
		{
			foreach (Tile allTile in AllTiles)
			{
				if ((Object)(object)allTile == (Object)(object)tile || (Object)(object)allTile == (Object)null)
				{
					continue;
				}
				GameObject val2 = null;
				try
				{
					val2 = allTile.Prefab;
				}
				catch
				{
				}
				if (!((Object)(object)val2 != (Object)(object)val) && num < allTile.AllDoorways.Count)
				{
					Doorway val3 = allTile.AllDoorways[num];
					if ((Object)(object)val3 != (Object)null && (Object)(object)val3.ConnectedDoorway == (Object)null && DonorHasVisuals(val3))
					{
						return val3;
					}
				}
			}
		}
		foreach (Tile caveTile in CaveTiles)
		{
			foreach (Doorway unusedDoorway in caveTile.UnusedDoorways)
			{
				if ((Object)(object)unusedDoorway == (Object)null || (Object)(object)unusedDoorway == (Object)(object)d || (Object)(object)unusedDoorway.ConnectedDoorway != (Object)null || (Object)(object)unusedDoorway.Socket != (Object)(object)d.Socket || !DonorHasVisuals(unusedDoorway))
				{
					continue;
				}
				return unusedDoorway;
			}
		}
		return null;
	}

	private static bool DonorHasVisuals(Doorway d)
	{
		GameObject val = (GameObject)((!(SpawnedBlockerField != null)) ? null : /*isinst with value type is only supported in some contexts*/);
		if ((Object)(object)val != (Object)null)
		{
			return true;
		}
		if (d.BlockerSceneObjects != null)
		{
			foreach (GameObject blockerSceneObject in d.BlockerSceneObjects)
			{
				if ((Object)(object)blockerSceneObject != (Object)null)
				{
					return true;
				}
			}
		}
		return false;
	}

	public bool IsSealed(int index)
	{
		return Sealed.ContainsKey(index);
	}

	public bool ApplySeal(int index)
	{
		if (Sealed.ContainsKey(index) || index < 0 || index >= Connections.Count)
		{
			return false;
		}
		DoorwayConnection val = Connections[index];
		if (val == null || (Object)(object)val.A == (Object)null || (Object)(object)val.B == (Object)null)
		{
			return false;
		}
		Seal seal = new Seal();
		bool flag = false;
		flag |= SealSide(val.A, index * 2, seal);
		if (!(flag | SealSide(val.B, index * 2 + 1, seal)))
		{
			RemoveSeal(seal);
			return false;
		}
		AddBarrier(val.A, seal);
		Sealed[index] = seal;
		Plugin.Verbose("Sealed opening " + index + " (" + ((Object)val.A.Tile).name + " <-> " + ((Object)val.B.Tile).name + ")");
		return true;
	}

	private bool SealSide(Doorway d, int seed, Seal seal)
	{
		//IL_00c0: 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_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		if (d.ConnectorSceneObjects != null)
		{
			foreach (GameObject connectorSceneObject in d.ConnectorSceneObjects)
			{
				if ((Object)(object)connectorSceneObject != (Object)null && connectorSceneObject.activeSelf)
				{
					connectorSceneObject.SetActive(false);
					seal.Hidden.Add(connectorSceneObject);
				}
			}
		}
		GameObject val = PickWeighted(d.BlockerPrefabWeights ?? new List<GameObjectWeight>(), new Random(seed * 7919 + 17));
		if ((Object)(object)val != (Object)null)
		{
			GameObject val2 = Object.Instantiate<GameObject>(val);
			val2.transform.SetParent(((Component)d).transform, false);
			val2.transform.localPosition = d.BlockerPrefabPositionOffset;
			val2.transform.localScale = Vector3.one;
			if (d.AvoidRotatingBlockerPrefab)
			{
				val2.transform.rotation = Quaternion.Euler(d.BlockerPrefabRotationOffset);
			}
			else
			{
				val2.transform.localRotation = Quaternion.Euler(d.BlockerPrefabRotationOffset);
			}
			Prepare(val2);
			seal.Spawned.Add(val2);
			return true;
		}
		Doorway val3 = FindDonor(d);
		if ((Object)(object)val3 == (Object)null)
		{
			return false;
		}
		bool result = false;
		GameObject val4 = (GameObject)((!(SpawnedBlockerField != null)) ? null : /*isinst with value type is only supported in some contexts*/);
		if ((Object)(object)val4 != (Object)null)
		{
			seal.Spawned.Add(CloneRelative(val4, ((Component)val3).transform, ((Component)d).transform));
			result = true;
		}
		if (val3.BlockerSceneObjects != null)
		{
			foreach (GameObject blockerSceneObject in val3.BlockerSceneObjects)
			{
				if (!((Object)(object)blockerSceneObject == (Object)null))
				{
					seal.Spawned.Add(CloneRelative(blockerSceneObject, ((Component)val3).transform, ((Component)d).transform));
					result = true;
				}
			}
		}
		return result;
	}

	private static GameObject CloneRelative(GameObject src, Transform from, Transform to)
	{
		//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_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_002d: 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_004a: 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)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_0103: Unknown result type (might be due to invalid IL or missing references)
		Vector3 val = from.InverseTransformPoint(src.transform.position);
		Quaternion val2 = Quaternion.Inverse(from.rotation) * src.transform.rotation;
		GameObject val3 = Object.Instantiate<GameObject>(src);
		val3.transform.SetParent(to, true);
		val3.transform.position = to.TransformPoint(val);
		val3.transform.rotation = to.rotation * val2;
		Vector3 lossyScale = src.transform.lossyScale;
		Vector3 lossyScale2 = to.lossyScale;
		val3.transform.localScale = new Vector3((lossyScale2.x == 0f) ? 1f : (lossyScale.x / lossyScale2.x), (lossyScale2.y == 0f) ? 1f : (lossyScale.y / lossyScale2.y), (lossyScale2.z == 0f) ? 1f : (lossyScale.z / lossyScale2.z));
		val3.SetActive(true);
		Prepare(val3);
		return val3;
	}

	private static void Prepare(GameObject inst)
	{
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//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_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		if (Plugin.DebugTint.Value)
		{
			Renderer[] componentsInChildren = inst.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val in componentsInChildren)
			{
				Material[] materials = val.materials;
				foreach (Material val2 in materials)
				{
					if (!((Object)(object)val2 == (Object)null))
					{
						if (val2.HasProperty("_BaseColor"))
						{
							val2.SetColor("_BaseColor", val2.GetColor("_BaseColor") * Tint);
						}
						if (val2.HasProperty("_Color"))
						{
							val2.SetColor("_Color", val2.GetColor("_Color") * Tint);
						}
						if (val2.HasProperty("_EmissiveColor"))
						{
							val2.SetColor("_EmissiveColor", new Color(0.6f, 0f, 0f, 1f));
							val2.EnableKeyword("_EMISSIVE_COLOR_MAP");
						}
					}
				}
			}
		}
		((Object)inst).name = ((Object)inst).name.Replace("(Clone)", string.Empty).Trim();
		NetworkObject[] componentsInChildren2 = inst.GetComponentsInChildren<NetworkObject>(true);
		foreach (NetworkObject val3 in componentsInChildren2)
		{
			Object.Destroy((Object)(object)val3);
		}
	}

	private static void AddBarrier(Doorway d, Seal seal)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		Vector2 val = SocketSize(d);
		GameObject val2 = new GameObject("ShifterBarrier");
		val2.transform.SetParent(((Component)d).transform, false);
		val2.transform.localPosition = new Vector3(0f, val.y * 0.5f, 0f);
		val2.transform.localRotation = Quaternion.identity;
		int num = LayerMask.NameToLayer("Room");
		if (num >= 0)
		{
			val2.layer = num;
		}
		BoxCollider val3 = val2.AddComponent<BoxCollider>();
		val3.size = new Vector3(val.x, val.y, 0.35f);
		NavMeshObstacle val4 = val2.AddComponent<NavMeshObstacle>();
		val4.shape = (NavMeshObstacleShape)1;
		val4.size = new Vector3(val.x, val.y, 0.6f);
		val4.carving = true;
		val4.carveOnlyStationary = false;
		seal.Spawned.Add(val2);
	}

	public void RemoveSeal(int index)
	{
		if (Sealed.TryGetValue(index, out var value))
		{
			Sealed.Remove(index);
			RemoveSeal(value);
			Plugin.Verbose("Reopened opening " + index);
		}
	}

	private static void RemoveSeal(Seal seal)
	{
		foreach (GameObject item in seal.Spawned)
		{
			if ((Object)(object)item != (Object)null)
			{
				Object.Destroy((Object)(object)item);
			}
		}
		foreach (GameObject item2 in seal.Hidden)
		{
			if ((Object)(object)item2 != (Object)null)
			{
				item2.SetActive(true);
			}
		}
		seal.Spawned.Clear();
		seal.Hidden.Clear();
	}

	public void ApplySet(ICollection<int> wanted)
	{
		List<int> list = new List<int>(Sealed.Keys);
		foreach (int item in list)
		{
			if (!wanted.Contains(item))
			{
				RemoveSeal(item);
			}
		}
		foreach (int item2 in wanted)
		{
			if (!Sealed.ContainsKey(item2))
			{
				ApplySeal(item2);
			}
		}
	}

	public void ClearAll()
	{
		List<int> list = new List<int>(Sealed.Keys);
		foreach (int item in list)
		{
			RemoveSeal(item);
		}
	}
}
[HarmonyPatch(typeof(RoundManager), "GenerateNewFloor")]
internal static class ForceMineshaftPatch
{
	private static IntWithRarity[] saved;

	private static SelectableLevel savedLevel;

	private static bool listedFlows;

	internal static int FindMineshaftIndex(RoundManager rm)
	{
		if (rm.dungeonFlowTypes == null)
		{
			return -1;
		}
		if (!listedFlows)
		{
			listedFlows = true;
			for (int i = 0; i < rm.dungeonFlowTypes.Length; i++)
			{
				DungeonFlow val = ((rm.dungeonFlowTypes[i] == null) ? null : rm.dungeonFlowTypes[i].dungeonFlow);
				Plugin.Verbose("Interior " + i + ": " + ((!((Object)(object)val != (Object)null)) ? "(none)" : ((Object)val).name));
			}
		}
		for (int j = 0; j < rm.dungeonFlowTypes.Length; j++)
		{
			DungeonFlow val2 = ((rm.dungeonFlowTypes[j] == null) ? null : rm.dungeonFlowTypes[j].dungeonFlow);
			if (!((Object)(object)val2 == (Object)null))
			{
				string text = ((Object)val2).name ?? string.Empty;
				if (text.IndexOf("Level3", StringComparison.OrdinalIgnoreCase) >= 0 || text.IndexOf("Mine", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return j;
				}
			}
		}
		return -1;
	}

	[HarmonyPrefix]
	private static void Prefix(RoundManager __instance)
	{
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Expected O, but got Unknown
		saved = null;
		savedLevel = null;
		if (Plugin.ForceMineshaft.Value && !((Object)(object)__instance.currentLevel == (Object)null))
		{
			int num = FindMineshaftIndex(__instance);
			if (num < 0)
			{
				Plugin.Log.LogWarning((object)"ForceMineshaft: couldn't find the Mineshaft interior (turn on VerboseLogging to list them).");
				return;
			}
			savedLevel = __instance.currentLevel;
			saved = savedLevel.dungeonFlowTypes;
			IntWithRarity val = new IntWithRarity(num, 100, (LevelAmbienceLibrary)null);
			savedLevel.dungeonFlowTypes = (IntWithRarity[])(object)new IntWithRarity[1] { val };
		}
	}

	[HarmonyPostfix]
	private static void Postfix(RoundManager __instance)
	{
		if ((Object)(object)savedLevel != (Object)null)
		{
			savedLevel.dungeonFlowTypes = saved;
			DungeonFlow val = ((!((Object)(object)__instance.dungeonGenerator != (Object)null) || __instance.dungeonGenerator.Generator == null) ? null : __instance.dungeonGenerator.Generator.DungeonFlow);
			Plugin.Log.LogInfo((object)("ForceMineshaft: generating " + ((!((Object)(object)val != (Object)null)) ? "?" : ((Object)val).name) + " (interior " + __instance.currentDungeonType + ")."));
		}
		saved = null;
		savedLevel = null;
	}
}
[DefaultExecutionOrder(10000)]
internal class ShifterManager : MonoBehaviour
{
	private enum Phase
	{
		Idle,
		Closing,
		Holding,
		Opening
	}

	private class Track
	{
		public float CaveTime;

		public float NextRollAt;

		public int LastReported;

		public Tile LastTile;

		public readonly List<int> Crumbs = new List<int>();
	}

	private const string MsgLoop = "427Team.Shifter.Loop";

	private const string MsgSwapDone = "427Team.Shifter.SwapDone";

	private const string MsgSwapCooldown = "427Team.Shifter.SwapCooldown";

	private float loopBlockedUntil;

	private float farBlockedUntil;

	private float myCaveTime;

	private int loopsArmed;

	private Tile myLastTile;

	private bool loopPending;

	private bool pendingFar;

	private float pendingSince;

	private Tile pendingInto;

	private Tile pendingTarget;

	private Doorway pendingEntry;

	private Doorway pendingTargetEntry;

	private List<KeyValuePair<Tile, Doorway>> pendingCandidates = new List<KeyValuePair<Tile, Doorway>>();

	private float loopCooldownUntil;

	private float guardUntil;

	private Vector3 guardOrigin;

	private Vector3 guardDest;

	private float guardOriginYaw;

	private Tile pendingLoopRoom;

	private static readonly FieldInfo TargetYRot = AccessTools.Field(typeof(PlayerControllerB), "targetYRot");

	private float lastRejectNote;

	private const string MsgState = "427Team.Shifter.State";

	private const string MsgRequest = "427Team.Shifter.Request";

	private DungeonState state;

	private CustomMessagingManager registeredOn;

	private List<int> pendingRemoteSet;

	private Phase phase;

	private readonly List<int> pendingSeal = new List<int>();

	private float eventEnd;

	private float forceOpenAt;

	private float cooldownUntil;

	private float nextTick;

	private float changeBudget;

	private float nextAheadPlan;

	private PlayerControllerB eventPlayer;

	private int shiftsThisRound;

	private readonly HashSet<int> herdRoute = new HashSet<int>();

	private readonly HashSet<int> herdSides = new HashSet<int>();

	private bool currentIsTease;

	private readonly Dictionary<PlayerControllerB, Track> tracks = new Dictionary<PlayerControllerB, Track>();

	private bool photoRunning;

	private Transform camOverride;

	private Vector3 camOverridePos;

	private Quaternion camOverrideRot;

	private static string Short(Tile t)
	{
		return (!((Object)(object)t != (Object)null)) ? "?" : ((Object)t).name.Replace("(Clone)", string.Empty).Trim();
	}

	private unsafe void ArmLoops(int count)
	{
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		if (!Plugin.SwapsOnlyDuringShifts.Value || (!Plugin.LoopEnabled.Value && count > 0))
		{
			return;
		}
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || !singleton.IsServer)
		{
			return;
		}
		PlayerControllerB[] allPlayerScripts = StartOfRound.Instance.allPlayerScripts;
		FastBufferWriter val2 = default(FastBufferWriter);
		foreach (PlayerControllerB val in allPlayerScripts)
		{
			if ((Object)(object)val == (Object)null || !val.isPlayerControlled)
			{
				continue;
			}
			if (count > 0 && (val.isPlayerDead || !val.isInsideFactory || !tracks.TryGetValue(val, out var value) || !state.IsCave(value.LastTile)))
			{
				continue;
			}
			if ((Object)(object)val == (Object)(object)StartOfRound.Instance.localPlayerController)
			{
				SetArmed(count);
			}
			else
			{
				((FastBufferWriter)(ref val2))..ctor(8, (Allocator)2, -1);
				try
				{
					byte[] bytes = BitConverter.GetBytes(count);
					((FastBufferWriter)(ref val2)).WriteBytesSafe(bytes, 4, 0);
					singleton.CustomMessagingManager.SendNamedMessage("427Team.Shifter.Loop", val.actualClientId, val2, (NetworkDelivery)2);
				}
				finally
				{
					((IDisposable)(*(FastBufferWriter*)(&val2))/*cast due to .constrained prefix*/).Dispose();
				}
			}
			if (count > 0)
			{
				Plugin.Chat($"Room swaps armed for {val.playerUsername} ({count}).");
			}
		}
	}

	private static float CooldownFor(bool far)
	{
		return (!far) ? Plugin.LoopGlobalCooldown.Value : Plugin.FarSwapGlobalCooldown.Value;
	}

	private bool Blocked(bool far)
	{
		return Time.time < ((!far) ? loopBlockedUntil : farBlockedUntil);
	}

	private void Block(bool far, float seconds)
	{
		float num = Time.time + seconds;
		if (far)
		{
			if (num > farBlockedUntil)
			{
				farBlockedUntil = num;
			}
		}
		else if (num > loopBlockedUntil)
		{
			loopBlockedUntil = num;
		}
		if (loopPending && pendingFar == far)
		{
			loopPending = false;
		}
	}

	private unsafe void StartGlobalSwapCooldown(bool far)
	{
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		float seconds = CooldownFor(far);
		Block(far, seconds);
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null)
		{
			return;
		}
		if (singleton.IsServer)
		{
			BroadcastSwapCooldown(far, seconds);
			return;
		}
		FastBufferWriter val = default(FastBufferWriter);
		((FastBufferWriter)(ref val))..ctor(4, (Allocator)2, -1);
		try
		{
			byte[] array = new byte[1] { (byte)(far ? 1u : 0u) };
			((FastBufferWriter)(ref val)).WriteBytesSafe(array, 1, 0);
			singleton.CustomMessagingManager.SendNamedMessage("427Team.Shifter.SwapDone", 0uL, val, (NetworkDelivery)2);
		}
		finally
		{
			((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
		}
	}

	private unsafe void BroadcastSwapCooldown(bool far, float seconds)
	{
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || !singleton.IsServer)
		{
			return;
		}
		List<ulong> list = new List<ulong>();
		foreach (ulong connectedClientsId in singleton.ConnectedClientsIds)
		{
			if (connectedClientsId != 0)
			{
				list.Add(connectedClientsId);
			}
		}
		if (list.Count == 0)
		{
			return;
		}
		FastBufferWriter val = default(FastBufferWriter);
		((FastBufferWriter)(ref val))..ctor(8, (Allocator)2, -1);
		try
		{
			byte[] array = new byte[5]
			{
				(byte)(far ? 1u : 0u),
				0,
				0,
				0,
				0
			};
			Buffer.BlockCopy(BitConverter.GetBytes(seconds), 0, array, 1, 4);
			((FastBufferWriter)(ref val)).WriteBytesSafe(array, 5, 0);
			singleton.CustomMessagingManager.SendNamedMessage("427Team.Shifter.SwapCooldown", (IReadOnlyList<ulong>)list, val, (NetworkDelivery)2);
		}
		finally
		{
			((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
		}
	}

	private void OnSwapDoneMessage(ulong sender, FastBufferReader reader)
	{
		NetworkManager singleton = NetworkManager.Singleton;
		if (!((Object)(object)singleton == (Object)null) && singleton.IsServer)
		{
			int num = ((FastBufferReader)(ref reader)).Length - ((FastBufferReader)(ref reader)).Position;
			bool flag = false;
			if (num >= 1)
			{
				byte[] array = new byte[num];
				((FastBufferReader)(ref reader)).ReadBytesSafe(ref array, num, 0);
				flag = array[0] == 1;
			}
			float num2 = CooldownFor(flag);
			Block(flag, num2);
			BroadcastSwapCooldown(flag, num2);
			Plugin.Chat(string.Format("A player was {0}. No {1} for anyone for {2:0}s.", (!flag) ? "looped" : "far-swapped", (!flag) ? "loops" : "far swaps", num2));
		}
	}

	private void OnSwapCooldownMessage(ulong sender, FastBufferReader reader)
	{
		if (sender == 0)
		{
			int num = ((FastBufferReader)(ref reader)).Length - ((FastBufferReader)(ref reader)).Position;
			if (num >= 5)
			{
				byte[] array = new byte[num];
				((FastBufferReader)(ref reader)).ReadBytesSafe(ref array, num, 0);
				Block(array[0] == 1, BitConverter.ToSingle(array, 1));
			}
		}
	}

	private void OnLoopMessage(ulong sender, FastBufferReader reader)
	{
		if (sender == 0)
		{
			int num = ((FastBufferReader)(ref reader)).Length - ((FastBufferReader)(ref reader)).Position;
			if (num >= 4)
			{
				byte[] value = new byte[num];
				((FastBufferReader)(ref reader)).ReadBytesSafe(ref value, num, 0);
				SetArmed(BitConverter.ToInt32(value, 0));
			}
		}
	}

	private void SetArmed(int n)
	{
		loopsArmed = Mathf.Max(0, n);
		if (loopsArmed == 0)
		{
			loopPending = false;
		}
	}

	private void SwapGuardTick()
	{
		//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)
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		if (Time.time > guardUntil)
		{
			return;
		}
		PlayerControllerB val = ((!((Object)(object)StartOfRound.Instance != (Object)null)) ? null : StartOfRound.Instance.localPlayerController);
		if ((Object)(object)val == (Object)null || val.isPlayerDead)
		{
			guardUntil = 0f;
			return;
		}
		float num = ((Component)val).transform.position.y - guardDest.y;
		bool flag = Time.time < guardUntil - 2.5f;
		bool flag2 = (Object)(object)val.thisController != (Object)null && !val.thisController.isGrounded;
		if ((flag && flag2 && num < -0.9f) || num < -6f)
		{
			guardUntil = 0f;
			Plugin.Log.LogError((object)"Swap safety net: player fell out of the level after a swap, putting them back.");
			Plugin.Chat("Swap safety net triggered - put you back.");
			CharacterController thisController = val.thisController;
			if ((Object)(object)thisController != (Object)null)
			{
				((Collider)thisController).enabled = false;
			}
			((Component)val).transform.position = guardOrigin;
			((Component)val).transform.localEulerAngles = new Vector3(0f, guardOriginYaw, 0f);
			if (TargetYRot != null)
			{
				TargetYRot.SetValue(val, guardOriginYaw);
			}
			val.serverPlayerPosition = guardOrigin;
			if ((Object)(object)thisController != (Object)null)
			{
				((Collider)thisController).enabled = true;
			}
			val.teleportedLastFrame = true;
			val.ResetFallGravity();
			Physics.SyncTransforms();
		}
	}

	private void LoopTick()
	{
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: 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_00f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_0301: Unknown result type (might be due to invalid IL or missing references)
		//IL_0328: Unknown result type (might be due to invalid IL or missing references)
		//IL_032a: Unknown result type (might be due to invalid IL or missing references)
		//IL_032c: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_034a: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
		SwapGuardTick();
		bool value = Plugin.OnlyLoops.Value;
		bool value2 = Plugin.OnlyFarSwaps.Value;
		bool flag = value || value2;
		bool value3 = Plugin.SwapsOnlyDuringShifts.Value;
		if (state == null || (!flag && (!Plugin.LoopEnabled.Value || (value3 && loopsArmed <= 0))))
		{
			loopPending = false;
			return;
		}
		PlayerControllerB val = ((!((Object)(object)StartOfRound.Instance != (Object)null)) ? null : StartOfRound.Instance.localPlayerController);
		if ((Object)(object)val == (Object)null || val.isPlayerDead || !val.isInsideFactory || val.inSpecialInteractAnimation || val.isClimbingLadder)
		{
			loopPending = false;
			return;
		}
		Vector3 position = ((Component)val).transform.position;
		Tile val2 = state.FindTile(position + Vector3.up * 0.5f, myLastTile);
		if (state.IsCave(val2))
		{
			myCaveTime += Time.deltaTime;
		}
		else
		{
			myCaveTime = Mathf.Max(0f, myCaveTime - Time.deltaTime * 2f);
		}
		bool flag2 = flag || myCaveTime >= Plugin.SwapMinCaveSeconds.Value;
		int index;
		if (loopPending)
		{
			if ((Object)(object)val2 != (Object)(object)pendingInto || Time.time - pendingSince > 8f || Blocked(pendingFar))
			{
				loopPending = false;
			}
			else if (DepthInside(pendingInto, pendingEntry, position) >= 1.5f && (Object)(object)val.thisController != (Object)null && val.thisController.isGrounded)
			{
				bool flag3 = false;
				for (int i = 0; i < 2; i++)
				{
					if (flag3)
					{
						break;
					}
					if (i == 1 && Plugin.SwapMaxNudge.Value <= 0f)
					{
						break;
					}
					foreach (KeyValuePair<Tile, Doorway> pendingCandidate in pendingCandidates)
					{
						if ((Object)(object)pendingCandidate.Key == (Object)null || state.IsSealed(ConnOf(pendingCandidate.Value)))
						{
							continue;
						}
						pendingTarget = pendingCandidate.Key;
						pendingTargetEntry = pendingCandidate.Value;
						Vector3 val3 = MapToTarget(((Component)val).transform.position);
						Vector3 best;
						if (i == 0)
						{
							if (!GeometryMatches(val, out var why))
							{
								NoteReject(why);
								continue;
							}
							if (!SpotIsSafe(val, val3, pendingTarget, out why))
							{
								NoteReject(why);
								continue;
							}
							best = val3;
						}
						else if (!FindStandableNear(val, val3, pendingTarget, out best))
						{
							NoteReject("no standable spot nearby");
							continue;
						}
						if (!SwapWouldBeInvisible(val, best - val3))
						{
							continue;
						}
						loopPending = false;
						DoSwap(val, best);
						flag3 = true;
						break;
					}
				}
			}
		}
		else if ((Object)(object)val2 != (Object)null && (Object)(object)myLastTile != (Object)null && (Object)(object)val2 != (Object)(object)myLastTile && Time.time >= loopCooldownUntil && flag2 && (!Blocked(far: false) || !Blocked(far: true)) && state.IsCave(val2) && state.IsCave(myLastTile) && state.TryGetConnection(myLastTile, val2, out index) && !state.IsSealed(index))
		{
			bool flag4;
			bool flag5;
			if (flag)
			{
				flag4 = value2 && (!value || Random.value < 0.5f);
				flag5 = !flag4;
			}
			else
			{
				float value4 = Random.value;
				float num = ((!Blocked(far: true)) ? Plugin.FarSwapChance.Value : 0f);
				float num2 = ((!Blocked(far: false)) ? Plugin.LoopChance.Value : 0f);
				flag4 = value4 < num;
				flag5 = !flag4 && value4 < num + num2;
			}
			if ((flag4 && Blocked(far: true)) || (flag5 && Blocked(far: false)))
			{
				flag4 = false;
				flag5 = false;
			}
			if (flag4 || flag5)
			{
				DoorwayConnection val4 = state.Connections[index];
				Doorway entry = ((!((Object)(object)val4.A.Tile == (Object)(object)val2)) ? val4.B : val4.A);
				List<KeyValuePair<Tile, Doorway>> list = ((!flag4) ? LoopCandidates(val2, entry, myLastTile) : FarCandidates(val2, entry, myLastTile));
				Tile val5 = ((list.Count <= 0) ? null : list[0].Key);
				Doorway val6 = ((list.Count <= 0) ? null : list[0].Value);
				if ((Object)(object)val5 != (Object)null)
				{
					pendingCandidates = list;
					lastRejectNote = 0f;
					loopPending = true;
					pendingFar = flag4;
					pendingLoopRoom = ((!flag4) ? myLastTile : null);
					pendingSince = Time.time;
					pendingInto = val2;
					pendingEntry = entry;
					pendingTarget = val5;
					pendingTargetEntry = val6;
				}
				else
				{
					Plugin.Verbose("Swap: no seamless copy of " + ((Object)val2).name + " right now.");
					if (flag)
					{
						Plugin.Chat((!flag4) ? $"No loop back into {Short(myLastTile)} from this {Short(val2)}." : $"No far copy of {Short(val2)} that matches.");
					}
				}
			}
		}
		if ((Object)(object)val2 != (Object)null)
		{
			myLastTile = val2;
		}
	}

	private int ConnOf(Doorway d)
	{
		if ((Object)(object)d == (Object)null || (Object)(object)d.ConnectedDoorway == (Object)null)
		{
			return -1;
		}
		int index;
		return (!state.TryGetConnection(d.Tile, d.ConnectedDoorway.Tile, out index)) ? (-1) : index;
	}

	private static GameObject PrefabOf(Tile t)
	{
		try
		{
			return (!((Object)(object)t != (Object)null)) ? null : t.Prefab;
		}
		catch
		{
			return null;
		}
	}

	private bool SlotDiffers(Doorway da, Doorway db)
	{
		bool flag = (Object)(object)da != (Object)null && (Object)(object)da.ConnectedDoorway != (Object)null && !state.IsSealed(ConnOf(da));
		bool flag2 = (Object)(object)db != (Object)null && (Object)(object)db.ConnectedDoorway != (Object)null && !state.IsSealed(ConnOf(db));
		if (flag != flag2)
		{
			return true;
		}
		if (!flag)
		{
			return false;
		}
		return !SameNeighbour(da, db);
	}

	private int CountDiffs(Tile a, Tile b)
	{
		int num = 0;
		for (int i = 0; i < a.AllDoorways.Count && i < b.AllDoorways.Count; i++)
		{
			if (SlotDiffers(a.AllDoorways[i], b.AllDoorways[i]))
			{
				num++;
			}
		}
		return num;
	}

	private static bool SameNeighbour(Doorway da, Doorway db)
	{
		if ((Object)(object)da == (Object)null || (Object)(object)db == (Object)null)
		{
			return (Object)(object)da == (Object)(object)db;
		}
		Doorway connectedDoorway = da.ConnectedDoorway;
		Doorway connectedDoorway2 = db.ConnectedDoorway;
		if ((Object)(object)connectedDoorway == (Object)null || (Object)(object)connectedDoorway2 == (Object)null)
		{
			return (Object)(object)connectedDoorway == (Object)(object)connectedDoorway2;
		}
		if ((Object)(object)PrefabOf(connectedDoorway.Tile) != (Object)(object)PrefabOf(connectedDoorway2.Tile) || (Object)(object)PrefabOf(connectedDoorway.Tile) == (Object)null)
		{
			return false;
		}
		return connectedDoorway.Tile.AllDoorways.IndexOf(connectedDoorway) == connectedDoorway2.Tile.AllDoorways.IndexOf(connectedDoorway2);
	}

	private Tile FindSameRoomLoop(Tile into, Doorway entry, Tile from, out Doorway twinEntry)
	{
		twinEntry = null;
		GameObject val = PrefabOf(into);
		int num = into.AllDoorways.IndexOf(entry);
		if ((Object)(object)val == (Object)null || num < 0)
		{
			return null;
		}
		if (Plugin.LoopCorridorsOnly.Value && CountConnected(into) != 2)
		{
			return null;
		}
		int num2 = int.MaxValue;
		List<KeyValuePair<Tile, Doorway>> list = new List<KeyValuePair<Tile, Doorway>>();
		foreach (Tile caveTile in state.CaveTiles)
		{
			if ((Object)(object)caveTile == (Object)null || (Object)(object)caveTile == (Object)(object)into || (Object)(object)caveTile == (Object)(object)from || (Object)(object)PrefabOf(caveTile) != (Object)(object)val || num >= caveTile.AllDoorways.Count || caveTile.AllDoorways.Count != into.AllDoorways.Count)
			{
				continue;
			}
			Doorway val2 = caveTile.AllDoorways[num];
			if ((Object)(object)val2 == (Object)null || (Object)(object)val2.ConnectedDoorway == (Object)null || state.IsSealed(ConnOf(val2)))
			{
				continue;
			}
			bool flag = false;
			for (int i = 0; i < caveTile.AllDoorways.Count; i++)
			{
				if (i != num)
				{
					Doorway val3 = caveTile.AllDoorways[i];
					if ((Object)(object)val3 != (Object)null && (Object)(object)val3.ConnectedDoorway != (Object)null && (Object)(object)val3.ConnectedDoorway.Tile == (Object)(object)from && !state.IsSealed(ConnOf(val3)))
					{
						flag = true;
					}
				}
			}
			if (flag)
			{
				int num3 = CountDiffs(into, caveTile);
				if (num3 < num2)
				{
					num2 = num3;
					list.Clear();
				}
				if (num3 == num2)
				{
					list.Add(new KeyValuePair<Tile, Doorway>(caveTile, val2));
				}
			}
		}
		if (list.Count == 0)
		{
			return null;
		}
		KeyValuePair<Tile, Doorway> keyValuePair = list[Random.Range(0, list.Count)];
		twinEntry = keyValuePair.Value;
		return keyValuePair.Key;
	}

	internal void LogSwapStats()
	{
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		foreach (DoorwayConnection connection in state.Connections)
		{
			if (connection == null || (Object)(object)connection.A == (Object)null || (Object)(object)connection.B == (Object)null)
			{
				continue;
			}
			for (int i = 0; i < 2; i++)
			{
				Doorway val = ((i != 0) ? connection.A : connection.B);
				Tile val2 = ((i != 0) ? connection.B.Tile : connection.A.Tile);
				Tile tile = val.Tile;
				if (state.IsCave(tile) && state.IsCave(val2))
				{
					num++;
					if ((Object)(object)FindSameRoomLoop(tile, val, val2, out var twinEntry) != (Object)null)
					{
						num2++;
					}
					if ((Object)(object)FindFarTwin(tile, val, val2, out twinEntry) != (Object)null)
					{
						num3++;
					}
				}
			}
		}
		string text = $"Swap spots in this interior: {num2} of {num} cave crossings can loop, {num3} can far-swap.";
		Plugin.Log.LogInfo((object)text);
		Plugin.Chat(text);
	}

	private void NoteReject(string why)
	{
		if (!(Time.time - lastRejectNote < 1f))
		{
			lastRejectNote = Time.time;
			Plugin.Verbose("Swap candidate rejected: " + why);
		}
	}

	private List<KeyValuePair<Tile, Doorway>> LoopCandidates(Tile into, Doorway entry, Tile from)
	{
		List<KeyValuePair<int, KeyValuePair<Tile, Doorway>>> list = new List<KeyValuePair<int, KeyValuePair<Tile, Doorway>>>();
		GameObject val = PrefabOf(into);
		int num = into.AllDoorways.IndexOf(entry);
		List<KeyValuePair<Tile, Doorway>> list2 = new List<KeyValuePair<Tile, Doorway>>();
		if ((Object)(object)val == (Object)null || num < 0)
		{
			return list2;
		}
		if (Plugin.LoopCorridorsOnly.Value && CountConnected(into) != 2)
		{
			return list2;
		}
		foreach (Tile caveTile in state.CaveTiles)
		{
			if ((Object)(object)caveTile == (Object)null || (Object)(object)caveTile == (Object)(object)into || (Object)(object)caveTile == (Object)(object)from || (Object)(object)PrefabOf(caveTile) != (Object)(object)val || num >= caveTile.AllDoorways.Count || caveTile.AllDoorways.Count != into.AllDoorways.Count)
			{
				continue;
			}
			Doorway val2 = caveTile.AllDoorways[num];
			if ((Object)(object)val2 == (Object)null || (Object)(object)val2.ConnectedDoorway == (Object)null || state.IsSealed(ConnOf(val2)))
			{
				continue;
			}
			bool flag = false;
			for (int i = 0; i < caveTile.AllDoorways.Count; i++)
			{
				if (i != num)
				{
					Doorway val3 = caveTile.AllDoorways[i];
					if ((Object)(object)val3 != (Object)null && (Object)(object)val3.ConnectedDoorway != (Object)null && (Object)(object)val3.ConnectedDoorway.Tile == (Object)(object)from && !state.IsSealed(ConnOf(val3)))
					{
						flag = true;
					}
				}
			}
			if (flag)
			{
				list.Add(new KeyValuePair<int, KeyValuePair<Tile, Doorway>>(CountDiffs(into, caveTile), new KeyValuePair<Tile, Doorway>(caveTile, val2)));
			}
		}
		list.Sort((KeyValuePair<int, KeyValuePair<Tile, Doorway>> x, KeyValuePair<int, KeyValuePair<Tile, Doorway>> y) => x.Key.CompareTo(y.Key));
		foreach (KeyValuePair<int, KeyValuePair<Tile, Doorway>> item in list)
		{
			if (list2.Count >= 6)
			{
				break;
			}
			list2.Add(item.Value);
		}
		return list2;
	}

	private List<KeyValuePair<Tile, Doorway>> FarCandidates(Tile into, Doorway entry, Tile from)
	{
		List<KeyValuePair<Tile, Doorway>> list = new List<KeyValuePair<Tile, Doorway>>();
		GameObject val = PrefabOf(into);
		int num = into.AllDoorways.IndexOf(entry);
		if ((Object)(object)val == (Object)null || num < 0)
		{
			return list;
		}
		Dictionary<Tile, int> dictionary = state.TileDistances(into);
		List<KeyValuePair<float, KeyValuePair<Tile, Doorway>>> list2 = new List<KeyValuePair<float, KeyValuePair<Tile, Doorway>>>();
		foreach (Tile caveTile in state.CaveTiles)
		{
			if (!((Object)(object)caveTile == (Object)null) && !((Object)(object)caveTile == (Object)(object)into) && !((Object)(object)caveTile == (Object)(object)from) && !((Object)(object)PrefabOf(caveTile) != (Object)(object)val) && num < caveTile.AllDoorways.Count && caveTile.AllDoorways.Count == into.AllDoorways.Count)
			{
				Doorway val2 = caveTile.AllDoorways[num];
				if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val2.ConnectedDoorway == (Object)null) && !state.IsSealed(ConnOf(val2)) && (!Plugin.LoopRequireMatchingBehind.Value || SameNeighbour(entry, val2)) && dictionary.TryGetValue(caveTile, out var value) && value >= Plugin.FarSwapMinRooms.Value)
				{
					float key = (float)CountDiffs(into, caveTile) + Random.value * 0.9f;
					list2.Add(new KeyValuePair<float, KeyValuePair<Tile, Doorway>>(key, new KeyValuePair<Tile, Doorway>(caveTile, val2)));
				}
			}
		}
		list2.Sort((KeyValuePair<float, KeyValuePair<Tile, Doorway>> x, KeyValuePair<float, KeyValuePair<Tile, Doorway>> y) => x.Key.CompareTo(y.Key));
		foreach (KeyValuePair<float, KeyValuePair<Tile, Doorway>> item in list2)
		{
			if (list.Count >= 8)
			{
				break;
			}
			list.Add(item.Value);
		}
		return list;
	}

	private Vector3 MapToTarget(Vector3 p)
	{
		//IL_0016: 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_001c: Unknown result type (might be due to invalid IL or missing references)
		return ((Component)pendingTarget).transform.TransformPoint(((Component)pendingInto).transform.InverseTransformPoint(p));
	}

	private bool GeometryMatches(PlayerControllerB me, out string why)
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: 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_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_0122: Unknown result type (might be due to invalid IL or missing references)
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_0167: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_0198: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: 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_01ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b8: 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_01bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d4: 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_020d: Unknown result type (might be due to invalid IL or missing references)
		//IL_020f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0219: 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_0223: Unknown result type (might be due to invalid IL or missing references)
		//IL_0228: Unknown result type (might be due to invalid IL or missing references)
		//IL_022a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0234: Unknown result type (might be due to invalid IL or missing references)
		//IL_0239: Unknown result type (might be due to invalid IL or missing references)
		//IL_023e: Unknown result type (might be due to invalid IL or missing references)
		//IL_028f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0295: Unknown result type (might be due to invalid IL or missing references)
		//IL_029a: Unknown result type (might be due to invalid IL or missing references)
		//IL_029f: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02be: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e6: Unknown result type (might be due to invalid IL or missing references)
		why = null;
		int collidersAndRoomMaskAndDefault = StartOfRound.Instance.collidersAndRoomMaskAndDefault;
		Transform transform = ((Component)pendingInto).transform;
		Transform transform2 = ((Component)pendingTarget).transform;
		Quaternion val = transform2.rotation * Quaternion.Inverse(transform.rotation);
		Vector3 position = ((Component)me).transform.position;
		Vector3 val2 = MapToTarget(position);
		Vector3[] array = (Vector3[])(object)new Vector3[9]
		{
			Vector3.zero,
			new Vector3(0.6f, 0f, 0f),
			new Vector3(-0.6f, 0f, 0f),
			new Vector3(0f, 0f, 0.6f),
			new Vector3(0f, 0f, -0.6f),
			new Vector3(1.2f, 0f, 1.2f),
			new Vector3(-1.2f, 0f, 1.2f),
			new Vector3(1.2f, 0f, -1.2f),
			new Vector3(-1.2f, 0f, -1.2f)
		};
		Vector3[] array2 = array;
		foreach (Vector3 val3 in array2)
		{
			Vector3 val4 = transform.TransformDirection(val3);
			if (!SameRay(position + val4 + Vector3.up * 1f, Vector3.down, val2 + val * val4 + Vector3.up * 1f, Vector3.down, 6f, collidersAndRoomMaskAndDefault, 0.12f))
			{
				why = "different floor";
				return false;
			}
		}
		if (!SameRay(position + Vector3.up * 1f, Vector3.up, val2 + Vector3.up * 1f, Vector3.up, 6f, collidersAndRoomMaskAndDefault, 0.25f))
		{
			why = "different ceiling";
			return false;
		}
		float[] array3 = new float[3] { 0.25f, 1f, 1.7f };
		for (int j = 0; j < 16; j++)
		{
			Vector3 val5 = Quaternion.Euler(0f, (float)j * 22.5f, 0f) * transform.forward;
			Vector3 db = val * val5;
			float[] array4 = array3;
			foreach (float num in array4)
			{
				if (!SameRay(position + Vector3.up * num, val5, val2 + Vector3.up * num, db, 6f, collidersAndRoomMaskAndDefault, 0.2f))
				{
					why = "different rocks/walls nearby";
					return false;
				}
			}
		}
		return true;
	}

	private static bool SameRay(Vector3 oa, Vector3 da, Vector3 ob, Vector3 db, float range, int mask, float tol)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: 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)
		RaycastHit val = default(RaycastHit);
		bool flag = Physics.Raycast(oa, da, ref val, range, mask, (QueryTriggerInteraction)1);
		RaycastHit val2 = default(RaycastHit);
		bool flag2 = Physics.Raycast(ob, db, ref val2, range, mask, (QueryTriggerInteraction)1);
		if (flag != flag2)
		{
			float num = ((!flag) ? ((RaycastHit)(ref val2)).distance : ((RaycastHit)(ref val)).distance);
			return num > range - 0.3f;
		}
		return !flag || Mathf.Abs(((RaycastHit)(ref val)).distance - ((RaycastHit)(ref val2)).distance) <= tol;
	}

	private static int CountConnected(Tile t)
	{
		int num = 0;
		foreach (Doorway allDoorway in t.AllDoorways)
		{
			if ((Object)(object)allDoorway != (Object)null && (Object)(object)allDoorway.ConnectedDoorway != (Object)null)
			{
				num++;
			}
		}
		return num;
	}

	private Tile FindFarTwin(Tile into, Doorway entry, Tile from, out Doorway twinEntry)
	{
		twinEntry = null;
		GameObject val = PrefabOf(into);
		int num = into.AllDoorways.IndexOf(entry);
		if ((Object)(object)val == (Object)null || num < 0)
		{
			return null;
		}
		Dictionary<Tile, int> dictionary = state.TileDistances(into);
		SortedDictionary<int, List<KeyValuePair<Tile, Doorway>>> sortedDictionary = new SortedDictionary<int, List<KeyValuePair<Tile, Doorway>>>();
		foreach (Tile caveTile in state.CaveTiles)
		{
			if ((Object)(object)caveTile == (Object)null || (Object)(object)caveTile == (Object)(object)into || (Object)(object)caveTile == (Object)(object)from || (Object)(object)PrefabOf(caveTile) != (Object)(object)val || num >= caveTile.AllDoorways.Count || caveTile.AllDoorways.Count != into.AllDoorways.Count)
			{
				continue;
			}
			Doorway val2 = caveTile.AllDoorways[num];
			if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val2.ConnectedDoorway == (Object)null) && !state.IsSealed(ConnOf(val2)) && (!Plugin.LoopRequireMatchingBehind.Value || SameNeighbour(entry, val2)) && dictionary.TryGetValue(caveTile, out var value) && value >= Plugin.FarSwapMinRooms.Value)
			{
				int key = CountDiffs(into, caveTile);
				if (!sortedDictionary.TryGetValue(key, out var value2))
				{
					value2 = (sortedDictionary[key] = new List<KeyValuePair<Tile, Doorway>>());
				}
				value2.Add(new KeyValuePair<Tile, Doorway>(caveTile, val2));
			}
		}
		using (SortedDictionary<int, List<KeyValuePair<Tile, Doorway>>>.Enumerator enumerator2 = sortedDictionary.GetEnumerator())
		{
			if (enumerator2.MoveNext())
			{
				KeyValuePair<int, List<KeyValuePair<Tile, Doorway>>> current2 = enumerator2.Current;
				KeyValuePair<Tile, Doorway> keyValuePair = current2.Value[Random.Range(0, current2.Value.Count)];
				twinEntry = keyValuePair.Value;
				return keyValuePair.Key;
			}
		}
		return null;
	}

	private bool SwapWouldBeInvisible(PlayerControllerB me, Vector3 nudge)
	{
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: 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_005a: 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_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: 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_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_016a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_018c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01af: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_01be: 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: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		Transform val = ((!((Object)(object)me.gameplayCamera != (Object)null)) ? ((Component)me).transform : ((Component)me.gameplayCamera).transform);
		Transform transform = ((Component)pendingInto).transform;
		Transform transform2 = ((Component)pendingTarget).transform;
		Vector3 eye = transform2.TransformPoint(transform.InverseTransformPoint(val.position)) + nudge;
		Quaternion val2 = transform2.rotation * Quaternion.Inverse(transform.rotation) * val.rotation;
		Vector3 fwd = val2 * Vector3.forward;
		int collidersAndRoomMask = StartOfRound.Instance.collidersAndRoomMask;
		for (int i = 0; i < pendingInto.AllDoorways.Count; i++)
		{
			Doorway val3 = pendingInto.AllDoorways[i];
			Doorway val4 = ((i >= pendingTarget.AllDoorways.Count) ? null : pendingTarget.AllDoorways[i]);
			if (!((Object)(object)val3 == (Object)null) && !((Object)(object)val4 == (Object)null) && SlotDiffers(val3, val4))
			{
				if (OpeningVisible(val.position, val.forward, val3, collidersAndRoomMask))
				{
					return false;
				}
				if (OpeningVisible(eye, fwd, val4, collidersAndRoomMask))
				{
					return false;
				}
			}
		}
		if (AnythingVisibleIn(pendingInto, val.position, val.forward, collidersAndRoomMask, me))
		{
			return false;
		}
		if (AnythingVisibleIn(pendingTarget, eye, fwd, collidersAndRoomMask, me))
		{
			return false;
		}
		Vector3 dest = transform2.TransformPoint(transform.InverseTransformPoint(((Component)me).transform.position)) + nudge;
		if (SomeoneWouldNotice(me, dest))
		{
			return false;
		}
		return true;
	}

	private static bool OpeningVisible(Vector3 eye, Vector3 fwd, Doorway d, int mask)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: 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_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: 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_008e: 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_009f: 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_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: 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_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: 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_00f8: 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_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//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_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: 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_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Unknown result type (might be due to invalid IL or missing references)
		Vector2 val = DungeonState.SocketSize(d);
		Vector3 val2 = ((Component)d).transform.position + ((Component)d).transform.up * (val.y * 0.5f);
		Vector3 val3 = ((Component)d).transform.right * (val.x * 0.45f);
		Vector3 val4 = ((Component)d).transform.up * (val.y * 0.45f);
		Vector3[] array = (Vector3[])(object)new Vector3[9]
		{
			val2,
			val2 + val3,
			val2 - val3,
			val2 + val4,
			val2 - val4,
			val2 + val3 + val4,
			val2 - val3 + val4,
			val2 + val3 - val4,
			val2 - val3 - val4
		};
		Vector3[] array2 = array;
		foreach (Vector3 p in array2)
		{
			if (Visible(eye, fwd, p, mask, 0.4f))
			{
				return true;
			}
		}
		return false;
	}

	private bool AnythingVisibleIn(Tile t, Vector3 eye, Vector3 fwd, int mask, PlayerControllerB me)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: 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_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: 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_008e: 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_01c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: 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_01dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		Bounds bounds = t.Bounds;
		((Bounds)(ref bounds)).Expand(1f);
		RoundManager instance = RoundManager.Instance;
		if ((Object)(object)instance != (Object)null && instance.SpawnedEnemies != null)
		{
			foreach (EnemyAI spawnedEnemy in instance.SpawnedEnemies)
			{
				if ((Object)(object)spawnedEnemy != (Object)null && !spawnedEnemy.isOutside && ((Bounds)(ref bounds)).Contains(((Component)spawnedEnemy).transform.position) && Visible(eye, fwd, ((Component)spawnedEnemy).transform.position + Vector3.up * 0.8f, mask, 0.6f))
				{
					return true;
				}
			}
		}
		PlayerControllerB[] allPlayerScripts = StartOfRound.Instance.allPlayerScripts;
		foreach (PlayerControllerB val in allPlayerScripts)
		{
			if ((Object)(object)val != (Object)null && (Object)(object)val != (Object)(object)me && val.isPlayerControlled && ((Bounds)(ref bounds)).Contains(((Component)val).transform.position) && Visible(eye, fwd, ((Component)val).transform.position + Vector3.up * 1.2f, mask, 0.6f))
			{
				return true;
			}
		}
		GrabbableObject[] array = Object.FindObjectsByType<GrabbableObject>((FindObjectsSortMode)0);
		foreach (GrabbableObject val2 in array)
		{
			if ((Object)(object)val2 != (Object)null && !val2.isHeld && !val2.isPocketed && ((Bounds)(ref bounds)).Contains(((Component)val2).transform.position) && Visible(eye, fwd, ((Component)val2).transform.position + Vector3.up * 0.1f, mask, 0.3f))
			{
				return true;
			}
		}
		return false;
	}

	private static bool Visible(Vector3 eye, Vector3 fwd, Vector3 p, int mask, float endGap)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_002d: 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_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		Vector3 val = p - eye;
		float magnitude = ((Vector3)(ref val)).magnitude;
		if (magnitude > Plugin.SwapViewDistance.Value || magnitude < 0.05f)
		{
			return false;
		}
		if (Vector3.Angle(fwd, val) > 75f)
		{
			return false;
		}
		Vector3 val2 = p - val / magnitude * Mathf.Min(endGap, magnitude * 0.5f);
		return !Physics.Linecast(eye, val2, mask, (QueryTriggerInteraction)1);
	}

	private static float DepthInside(Tile tile, Doorway entry, Vector3 pos)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: 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)
		Vector3 forward = ((Component)entry).transform.forward;
		forward.y = 0f;
		if (((Vector3)(ref forward)).sqrMagnitude < 0.01f)
		{
			return 0f;
		}
		((Vector3)(ref forward)).Normalize();
		Bounds bounds = tile.Bounds;
		Vector3 val = ((Bounds)(ref bounds)).center - ((Component)entry).transform.position;
		val.y = 0f;
		Vector3 val2 = ((!(Vector3.Dot(forward, val) >= 0f)) ? (-forward) : forward);
		Vector3 val3 = pos - ((Component)entry).transform.position;
		val3.y = 0f;
		return Vector3.Dot(val3, val2);
	}

	private bool SpotIsSafe(PlayerControllerB me, Vector3 dest, Tile mustBe, out string why)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0137: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: 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_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: Unknown result type (might be due to invalid IL or missing references)
		//IL_014a: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0171: Unknown result type (might be due to invalid IL or missing references)
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0182: Unknown result type (might be due to invalid IL or missing references)
		//IL_018c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0191: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_0198: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01af: Unknown result type (might be due to invalid IL or missing references)
		why = null;
		Tile val = state.FloorTile(dest);
		if ((Object)(object)val != (Object)(object)mustBe)
		{
			why = "floor there belongs to " + ((!((Object)(object)val != (Object)null)) ? "nothing" : ((Object)val).name);
			return false;
		}
		int collidersAndRoomMaskAndDefault = StartOfRound.Instance.collidersAndRoomMaskAndDefault;
		RaycastHit val2 = default(RaycastHit);
		if (!Physics.Raycast(dest + Vector3.up * 0.5f, Vector3.down, ref val2, 2f, collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1) || ((RaycastHit)(ref val2)).distance > 0.65f)
		{
			why = "no floor under the feet";
			return false;
		}
		if (((RaycastHit)(ref val2)).normal.y < 0.7f)
		{
			why = "floor too steep";
			return false;
		}
		CharacterController thisController = me.thisController;
		float num = ((!((Object)(object)thisController != (Object)null)) ? 0.4f : (thisController.radius + 0.05f));
		float num2 = ((!((Object)(object)thisController != (Object)null)) ? 2.4f : thisController.height);
		Vector3 val3 = (Vector3)((!((Object)(object)thisController != (Object)null)) ? new Vector3(0f, 1.2f, 0f) : thisController.center);
		Vector3 val4 = dest + ((Component)me).transform.rotation * val3;
		float num3 = Mathf.Max(0f, num2 * 0.5f - num);
		Vector3 val5 = val4 - Vector3.up * num3 + Vector3.up * 0.08f;
		Vector3 val6 = val4 + Vector3.up * num3;
		if (Physics.CheckCapsule(val5, val6, num, StartOfRound.Instance.collidersAndRoomMask, (QueryTriggerInteraction)1))
		{
			why = "no room to stand";
			return false;
		}
		return true;
	}

	private bool FindStandableNear(PlayerControllerB me, Vector3 exact, Tile target, out Vector3 best)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: 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_014e: 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_016a: Unknown result type (might be due to invalid IL or missing references)
		best = exact;
		float value = Plugin.SwapMaxNudge.Value;
		int collidersAndRoomMaskAndDefault = StartOfRound.Instance.collidersAndRoomMaskAndDefault;
		RaycastHit val2 = default(RaycastHit);
		for (float num = 0f; num <= value + 0.001f; num += 0.25f)
		{
			int num2 = ((num < 0.01f) ? 1 : Mathf.Max(8, Mathf.RoundToInt((float)Math.PI * 2f * num / 0.25f)));
			float num3 = float.MaxValue;
			bool flag = false;
			for (int i = 0; i < num2; i++)
			{
				float num4 = (float)i * 360f / (float)num2;
				Vector3 val = exact + Quaternion.Euler(0f, num4, 0f) * Vector3.forward * num;
				if (!Physics.Raycast(val + Vector3.up * 1.2f, Vector3.down, ref val2, 2.6f, collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1) || ((RaycastHit)(ref val2)).normal.y < 0.7f)
				{
					continue;
				}
				Vector3 val3 = ((RaycastHit)(ref val2)).point + Vector3.up * 0.02f;
				if (!(Mathf.Abs(val3.y - exact.y) > 1.2f) && SpotIsSafe(me, val3, target, out var _))
				{
					Vector3 val4 = val3 - exact;
					float sqrMagnitude = ((Vector3)(ref val4)).sqrMagnitude;
					if (sqrMagnitude < num3)
					{
						num3 = sqrMagnitude;
						best = val3;
						flag = true;
					}
				}
			}
			if (flag)
			{
				return true;
			}
		}
		return false;
	}

	private void DoSwap(PlayerControllerB me, Vector3 dest)
	{
		//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_0027: 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_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: 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_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_018a: 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_01c3: 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_0208: 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)
		Transform transform = ((Component)pendingInto).transform;
		Transform transform2 = ((Component)pendingTarget).transform;
		float num = transform2.eulerAngles.y - transform.eulerAngles.y;
		float num2 = ((Component)me).transform.eulerAngles.y + num;
		if ((Object)(object)state.FloorTile(((Component)me).transform.position) != (Object)(object)pendingInto)
		{
			Plugin.Log.LogWarning((object)("Swap cancelled: player isn't standing in " + ((Object)pendingInto).name + " after all."));
			return;
		}
		if (!SpotIsSafe(me, dest, pendingTarget, out var why))
		{
			Plugin.Log.LogWarning((object)("Swap cancelled, unsafe destination in " + ((Object)pendingTarget).name + ": " + why + " at " + dest));
			Plugin.Chat("Swap cancelled (unsafe spot: " + why + ").");
			return;
		}
		Plugin.Verbose($"Swap {((Object)pendingInto).name} {((Component)me).transform.position} -> {((Object)pendingTarget).name} {dest}");
		guardOrigin = ((Component)me).transform.position;
		guardOriginYaw = ((Component)me).transform.eulerAngles.y;
		guardDest = dest;
		guardUntil = Time.time + 3f;
		CharacterController thisController = me.thisController;
		if ((Object)(object)thisController != (Object)null)
		{
			((Collider)thisController).enabled = false;
		}
		((Component)me).transform.position = dest;
		if (TargetYRot != null)
		{
			TargetYRot.SetValue(me, num2);
		}
		((Component)me).transform.localEulerAngles = new Vector3(0f, num2, 0f);
		me.serverPlayerPosition = dest;
		if ((Object)(object)thisController != (Object)null)
		{
			((Collider)thisController).enabled = true;
		}
		me.teleportedLastFrame = true;
		Physics.SyncTransforms();
		myLastTile = pendingTarget;
		bool flag = Plugin.OnlyLoops.Value || Plugin.OnlyFarSwaps.Value;
		if (Plugin.SwapsOnlyDuringShifts.Value && loopsArmed > 0 && !flag)
		{
			loopsArmed--;
		}
		StartGlobalSwapCooldown(pendingFar);
		Plugin.Chat(string.Format("No {0} for anyone for {1:0}s.", (!pendingFar) ? "loops" : "far swaps", CooldownFor(pendingFar)));
		loopCooldownUntil = Time.time + ((!flag) ? 8f : 2f);
		string text = (flag ? "debug mode" : ((!Plugin.SwapsOnlyDuringShifts.Value) ? "any time" : (loopsArmed + " left this shift")));
		if (pendingFar)
		{
			Plugin.Log.LogInfo((object)$"far swap: {me.playerUsername} -> another {((Object)pendingTarget).name} ({state.TileDistance(pendingInto, pendingTarget)} rooms away).");
			Plugin.Chat($"Far swap into another {Short(pendingTarget)}, {state.TileDistance(pendingInto, pendingTarget)} rooms away ({text}).");
		}
		else
		{
			Plugin.Log.LogInfo((object)string.Format("loop: {0} -> other {1}, which leads back into {2}.", me.playerUsername, ((Object)pendingTarget).name, (!((Object)(object)pendingLoopRoom != (Object)null)) ? "?" : ((Object)pendingLoopRoom).name));
			Plugin.Chat($"Loop: keep walking and you're back in the {Short(pendingLoopRoom)} you just left ({text}).");
		}
	}

	private bool SomeoneWouldNotice(PlayerControllerB me, Vector3 dest)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: 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_00fb: 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_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_011a: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		int collidersAndRoomMask = StartOfRound.Instance.collidersAndRoomMask;
		Vector3 val = ((Component)me).transform.position + Vector3.up * 1.2f;
		Vector3 val2 = dest + Vector3.up * 1.2f;
		PlayerControllerB[] allPlayerScripts = StartOfRound.Instance.allPlayerScripts;
		foreach (PlayerControllerB val3 in allPlayerScripts)
		{
			if (!((Object)(object)val3 == (Object)null) && !((Object)(object)val3 == (Object)(object)me) && val3.isPlayerControlled && !val3.isPlayerDead && val3.isInsideFactory)
			{
				Transform val4 = ((!((Object)(object)val3.gameplayCamera != (Object)null)) ? ((Component)val3).transform : ((Component)val3.gameplayCamera).transform);
				if (Vector3.Distance(val4.position, val) < 1.5f || Vector3.Distance(val4.position, val2) < 1.5f)
				{
					return true;
				}
				if (Visible(val4.position, val4.forward, val, collidersAndRoomMask, 0.6f) || Visible(val4.position, val4.forward, val2, collidersAndRoomMask, 0.6f))
				{
					return true;
				}
			}
		}
		return false;
	}

	private void Update()
	{
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Expected O, but got Unknown
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Expected O, but got Unknown
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Expected O, but got Unknown
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Expected O, but got Unknown
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Expected O, but got Unknown
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || !singleton.IsListening || singleton.CustomMessagingManager == null)
		{
			registeredOn = null;
			Teardown();
			pendingRemoteSet = null;
			return;
		}
		if (registeredOn != singleton.CustomMessagingManager)
		{
			registeredOn = singleton.CustomMessagingManager;
			registeredOn.RegisterNamedMessageHandler("427Team.Shifter.State", new HandleNamedMessageDelegate(OnStateMessage));
			registeredOn.RegisterNamedMessageHandler("427Team.Shifter.Request", new HandleNamedMessageDelegate(OnRequestMessage));
			registeredOn.RegisterNamedMessageHandler("427Team.Shifter.Loop", new HandleNamedMessageDelegate(OnLoopMessage));
			registeredOn.RegisterNamedMessageHandler("427Team.Shifter.SwapDone", new HandleNamedMessageDelegate(OnSwapDoneMessage));
			registeredOn.RegisterNamedMessageHandler("427Team.Shifter.SwapCooldown", new HandleNamedMessageDelegate(OnSwapCooldownMessage));
		}
		Dungeon val = CurrentDungeon();
		if ((Object)(object)val == (Object)null)
		{
			Teardown();
			return;
		}
		if (state == null || (Object)(object)state.Dungeon != (Object)(object)val)
		{
			Teardown();
			try
			{
				state = new DungeonState(val);
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Failed to scan interior: " + ex));
				state = null;
				return;
			}
			Plugin.Chat($"Interior ready: {state.CaveTiles.Count} cave tiles, {state.Sealable.Count} openings can shift.");
			try
			{
				LogSwapStats();
			}
			catch (Exception ex2)
			{
				Plugin.Log.LogWarning((object)("Swap stats failed: " + ex2.Message));
			}
			if (singleton.IsServer)
			{
				ResetHost();
			}
			else
			{
				if (pendingRemoteSet != null)
				{
					state.ApplySet(pendingRemoteSet);
					pendingRemoteSet = null;
				}
				SendRequest();
			}
		}
		if (singleton.IsServer)
		{
			HostTick();
		}
		LoopTick();
		DebugKeysTick(singleton);
	}

	private void LateUpdate()
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)camOverride != (Object)null)
		{
			camOverride.position = camOverridePos;
			camOverride.rotation = camOverrideRot;
		}
	}

	private static Dungeon CurrentDungeon()
	{
		RoundManager instance = RoundManager.Instance;
		StartOfRound instance2 = StartOfRound.Instance;
		if ((Object)(object)instance == (Object)null || (Object)(object)instance2 == (Object)null || instance2.inShipPhase)
		{
			return null;
		}
		if (!instance.dungeonCompletedGenerating || (Object)(object)instance.dungeonGenerator == (Object)null || instance.dungeonGenerator.Generator == null)
		{
			return null;
		}
		Dungeon currentDungeon = instance.dungeonGenerator.Generator.CurrentDungeon;
		return (!((Object)(object)currentDungeon != (Object)null) || !((Object)(object)((Component)currentDungeon).gameObject != (Object)null)) ? null : currentDungeon;
	}

	private void Teardown()
	{
		loopsArmed = 0;
		loopPending = false;
		loopBlockedUntil = 0f;
		farBlockedUntil = 0f;
		myCaveTime = 0f;
		myLastTile = null;
		if (state != null)
		{
			try
			{
				state.ClearAll();
			}
			catch
			{
			}
			state = null;
		}
		ResetHost();
	}

	private void ResetHost()
	{
		shiftsThisRound = 0;
		currentIsTease = false;
		phase = Phase.Idle;
		pendingSeal.Clear();
		tracks.Clear();
		cooldownUntil = Time.time + 30f;
	}

	private unsafe void Broadcast()
	{
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || !singleton.IsServer || state == null)
		{
			return;
		}
		List<ulong> list = new List<ulong>();
		foreach (ulong connectedClientsId in singleton.ConnectedClientsIds)
		{
			if (connectedClientsId != 0)
			{
				list.Add(connectedClientsId);
			}
		}
		if (list.Count == 0)
		{
			return;
		}
		byte[] array = Encode(state.Sealed.Keys);
		FastBufferWriter val = default(FastBufferWriter);
		((FastBufferWriter)(ref val))..ctor(array.Length + 8, (Allocator)2, -1);
		try
		{
			((FastBufferWriter)(ref val)).WriteBytesSafe(array, array.Length, 0);
			singleton.CustomMessagingManager.SendNamedMessage("427Team.Shifter.State", (IReadOnlyList<ulong>)list, val, (NetworkDelivery)3);
		}
		finally
		{
			((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
		}
	}

	private unsafe void SendStateTo(ulong client)
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(ob