Decompiled source of NovaKvasturSorter v0.9.24

plugins/NovaKvasturSorter.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("NovaKvasturSorter")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.9.24.0")]
[assembly: AssemblyInformationalVersion("0.9.24")]
[assembly: AssemblyProduct("NovaKvasturSorter")]
[assembly: AssemblyTitle("NovaKvasturSorter")]
[assembly: AssemblyVersion("0.9.24.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace NovaKvasturSorter
{
	internal sealed class Kvastur : MonoBehaviour, Hoverable, Interactable
	{
		private sealed class Job
		{
			public CraftingStation Workbench;

			public Container Source;

			public Container Destination;

			public ItemData Item;

			public bool Returning;

			public ItemDrop FloorDrop;

			public bool DirectPickup;

			public bool SpeechAttempted;
		}

		private enum DeliverySearch
		{
			Pending,
			Found,
			Full,
			Unavailable
		}

		private enum WorkStatus
		{
			Idle,
			Floor,
			Box,
			Delivering,
			Returning,
			RouteBlocked,
			WaitingForBox,
			NoWorkbench,
			WaitingForChest,
			Drinking,
			Planning
		}

		private enum SpeechLine
		{
			Stones = 1,
			Work,
			Full,
			BonesAndFood,
			Done,
			Rare,
			FormerOwner,
			Coal,
			Iron,
			RockLoad,
			DoorBlocked,
			SweepDone,
			Nausea1,
			Nausea2,
			Raid1,
			Raid2,
			Raid3,
			Boar,
			Wolf,
			Lox,
			Asksvin,
			Moose,
			Hen
		}

		private const string ConfirmSourceRpc = "NovaKvasturSorter_ConfirmCargoSource";

		private const string SourceMissingRpc = "NovaKvasturSorter_CargoSourceMissing";

		private static readonly FieldRef<ZDOMan, Dictionary<ZDOID, long>> DestroyedObjects = AccessTools.FieldRefAccess<ZDOMan, Dictionary<ZDOID, long>>("m_deadZDOs");

		private float nextSourceCheck;

		private ZDOID confirmedMissingSource;

		private const string WorkbenchName = "$piece_workbench";

		private const float WalkSpeed = 2.5f;

		private const float SearchInterval = 1f;

		private const float ContainerReachHeight = 2.55f;

		private const string CargoKey = "KvasturSorter.Cargo";

		private static readonly int FeetKey = StringExtensionMethods.GetStableHashCode("KvasturSorter.Feet");

		private static readonly int SortingBoxPrefab = StringExtensionMethods.GetStableHashCode("KvasturSorter_Box");

		private static readonly HashSet<int> ChestPrefabs = new HashSet<int>
		{
			StringExtensionMethods.GetStableHashCode("piece_chest_wood"),
			StringExtensionMethods.GetStableHashCode("piece_chest"),
			StringExtensionMethods.GetStableHashCode("piece_chest_blackmetal"),
			StringExtensionMethods.GetStableHashCode("piece_chest_grausten"),
			StringExtensionMethods.GetStableHashCode("piece_chest_barrel")
		};

		private static Container[] chests = Array.Empty<Container>();

		private static CraftingStation[] stations = Array.Empty<CraftingStation>();

		private static float nextChestRefresh;

		private static float nextStationRefresh;

		private static long visitOrder;

		private readonly List<DoorRoutes.Step> route = new List<DoorRoutes.Step>();

		private readonly List<DoorRoutes.Step> candidateRoute = new List<DoorRoutes.Step>();

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

		private readonly List<ZDO> cargoSourceCandidates = new List<ZDO>();

		private readonly Dictionary<Container, (float Until, Vector3 Start)> failedDeliveries = new Dictionary<Container, (float, Vector3)>();

		private IEnumerator<DeliverySearch> deliverySearch;

		private Transform visual;

		private BoxCollider bodyCollider;

		private Vector3 bodyColliderCenter;

		private ZNetView view;

		private Door crossingDoor;

		private float doorDeadline;

		private bool doorAcquired;

		private bool doorLease;

		private int doorWaypoint;

		private Vector3 feet;

		private Job job;

		private Inventory cargo;

		private ZDOID cargoSource;

		private Vector3? cargoSourcePosition;

		private bool cargoLoaded;

		private int waypoint;

		private float nextSearch;

		private string lastStatus;

		private float nextPositionSync;

		private float remoteWalkingUntil;

		private bool remotePositionLoaded;

		private static readonly Func<Container, bool> RefreshContainerInventory = AccessTools.MethodDelegate<Func<Container, bool>>(AccessTools.Method(typeof(Container), "Load", (Type[])null, (Type[])null), (object)null, true);

		private const int BoostMinutes = 5;

		private const long BoostDuration = 3000000000L;

		private const long DrinkPause = 8000000L;

		private const float BoostedSpeed = 4.5f;

		private const int BoostCargoSize = 4;

		private static readonly int BoostUntilKey = StringExtensionMethods.GetStableHashCode("KvasturSorter.TastyMeadUntil");

		private bool directFloorCargo;

		private Transform drinkProp;

		private const float HoverRadius = 0.85f;

		private const float ControlRange = 5f;

		private const string ToggleRpc = "NovaKvasturSorter_FloorPickup";

		private static readonly int FloorDisabledKey = StringExtensionMethods.GetStableHashCode("NovaKvasturSorter.FloorPickupDisabled");

		private static readonly int WorkStatusKey = StringExtensionMethods.GetStableHashCode("NovaKvasturSorter.WorkStatus");

		private static readonly int PlayerPrefab = StringExtensionMethods.GetStableHashCode("Player");

		private static readonly int SightLayers = LayerMask.GetMask(new string[4] { "piece", "Default", "static_solid", "terrain" });

		private static readonly HashSet<Kvastur> ActiveHelpers = new HashSet<Kvastur>();

		private static Dictionary<string, string[]> workLabels;

		private WorkStatus workHint;

		private float workHintUntil;

		private static readonly int ForwardSpeed = Animator.StringToHash("forward_speed");

		private static readonly int ItemLayers = LayerMask.GetMask(new string[1] { "item" });

		private static readonly int RoofLayers = LayerMask.GetMask(new string[3] { "piece", "static_solid", "Default" });

		private static Collider[] floorHits = (Collider[])(object)new Collider[64];

		private readonly Dictionary<(ItemDrop Drop, CraftingStation Station), float> failedFloorRoutes = new Dictionary<(ItemDrop, CraftingStation), float>();

		private Animator animator;

		private bool walking;

		private bool floorCargo;

		private bool sweepingFloor;

		private float nextFloorSearch;

		private float nextIdleWalk;

		private float nextShelterSearch;

		private Vector3? lastShelter;

		private int shelterProbe;

		private const string SpeechRpc = "NovaKvasturSorter_Speak";

		private const float SpeechRange = 12f;

		private const int RareSpeechRolls = 200;

		private const int OwnerSpeechRolls = 1000;

		private static readonly Random SpeechRandom = new Random();

		private float nextSpeech;

		private float nextAmbientCheck;

		private RandomEvent lastRaid;

		private bool lastNausea;

		private int nearbyPets;

		private bool sweptLoot;

		internal Vector3 DropPosition
		{
			get
			{
				//IL_001f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0013: Unknown result type (might be due to invalid IL or missing references)
				if (!Object.op_Implicit((Object)(object)visual))
				{
					return ((Component)this).transform.position;
				}
				return visual.position;
			}
		}

		private Vector3 DeliveryOrigin
		{
			get
			{
				//IL_002e: 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)
				if (!Object.op_Implicit((Object)(object)job.Source))
				{
					return ((Component)this).transform.position;
				}
				return ((Component)job.Source).transform.position;
			}
		}

		private long BoostRemaining
		{
			get
			{
				if (!Object.op_Implicit((Object)(object)view) || !view.IsValid() || !Object.op_Implicit((Object)(object)ZNet.instance))
				{
					return 0L;
				}
				return view.GetZDO().GetLong(BoostUntilKey, 0L) - ZNet.instance.GetTime().Ticks;
			}
		}

		private bool Boosted => BoostRemaining > 0;

		private bool Drinking => BoostRemaining > 2992000000u;

		private float MovementSpeed
		{
			get
			{
				if (!Boosted)
				{
					return 2.5f;
				}
				return 4.5f;
			}
		}

		private bool FloorPickupEnabled
		{
			get
			{
				if (Object.op_Implicit((Object)(object)view) && view.IsValid())
				{
					return !view.GetZDO().GetBool(FloorDisabledKey, false);
				}
				return true;
			}
		}

		internal static void InstallCargoRecovery(Harmony harmony)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ZNet), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Kvastur), "RegisterCargoRecovery", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static void RegisterCargoRecovery()
		{
			if (Object.op_Implicit((Object)(object)ZNet.instance) && ZNet.instance.IsServer())
			{
				ZRoutedRpc.instance.Register<ZDOID, ZDOID>("NovaKvasturSorter_ConfirmCargoSource", (Action<long, ZDOID, ZDOID>)ConfirmCargoSource);
			}
		}

		private static void ConfirmCargoSource(long sender, ZDOID helperId, ZDOID sourceId)
		{
			//IL_001f: 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_0032: 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_0076: 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)
			if (Object.op_Implicit((Object)(object)ZNet.instance) && ZNet.instance.IsServer() && ZDOMan.instance != null && !(sourceId == ZDOID.None))
			{
				ZDO zDO = ZDOMan.instance.GetZDO(helperId);
				if (zDO != null && zDO.GetOwner() == sender && zDO.GetPrefab() == StringExtensionMethods.GetStableHashCode("KvasturSorter_Helper") && DestroyedObjects.Invoke(ZDOMan.instance).ContainsKey(sourceId))
				{
					ZRoutedRpc.instance.InvokeRoutedRPC(sender, helperId, "NovaKvasturSorter_CargoSourceMissing", new object[1] { sourceId });
				}
			}
		}

		private void ReceiveMissingSource(long sender, ZDOID sourceId)
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: 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)
			if (Object.op_Implicit((Object)(object)view) && view.IsValid() && view.IsOwner() && Object.op_Implicit((Object)(object)ZNet.instance))
			{
				ZNetPeer serverPeer = ZNet.instance.GetServerPeer();
				if (serverPeer != null && serverPeer.m_uid == sender && sourceId == cargoSource)
				{
					confirmedMissingSource = sourceId;
				}
			}
		}

		private bool IsCargoSourceMissing()
		{
			//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_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)ZNet.instance) || ZDOMan.instance == null)
			{
				return false;
			}
			if (ZNet.instance.IsServer())
			{
				return !CargoSourceExists();
			}
			if (cargoSource != ZDOID.None && confirmedMissingSource == cargoSource)
			{
				return true;
			}
			if (Time.time >= nextSourceCheck && ZRoutedRpc.instance != null && ZNet.instance.GetServerPeer() != null)
			{
				nextSourceCheck = Time.time + 3f;
				ZRoutedRpc.instance.InvokeRoutedRPC("NovaKvasturSorter_ConfirmCargoSource", new object[2]
				{
					view.GetZDO().m_uid,
					cargoSource
				});
			}
			return false;
		}

		private bool HasWorkAccess(Vector3 point)
		{
			//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_0011: Unknown result type (might be due to invalid IL or missing references)
			return WardAccess.CheckShared(((Component)this).transform.position, feet, point);
		}

		private void Awake()
		{
			//IL_0046: 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_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			visual = ((Component)this).transform.Find("KvasturVisual");
			animator = ((Component)visual).GetComponent<Animator>();
			view = ((Component)this).GetComponent<ZNetView>();
			bodyCollider = ((Component)this).GetComponent<BoxCollider>();
			bodyColliderCenter = bodyCollider.center;
			feet = ((Component)this).transform.position;
			WearNTear component = ((Component)this).GetComponent<WearNTear>();
			component.m_onDestroyed = (Action)Delegate.Combine(component.m_onDestroyed, new Action(DropCargo));
			RegisterFloorToggle();
			if (Object.op_Implicit((Object)(object)view) && view.IsValid())
			{
				view.Register<ZDOID>("NovaKvasturSorter_CargoSourceMissing", (Action<long, ZDOID>)ReceiveMissingSource);
			}
		}

		private void Update()
		{
			//IL_009c: 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_00ad: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)view) || !view.IsValid() || !Object.op_Implicit((Object)(object)ZNet.instance))
			{
				return;
			}
			bool drinking = Drinking;
			UpdateDrinkVisual(drinking);
			if (!view.IsOwner())
			{
				if (job != null || route.Count != 0)
				{
					FinishJob();
				}
				cargoLoaded = false;
				sweepingFloor = false;
				failedFloorRoutes.Clear();
				UpdateRemotePosition();
				return;
			}
			if (!cargoLoaded)
			{
				feet = view.GetZDO().GetVec3(FeetKey, ((Component)this).transform.position);
				SetVisualPosition(feet);
				remotePositionLoaded = true;
			}
			CheckAmbientSpeech();
			LoadCargo();
			if (!FloorPickupEnabled && job?.FloorDrop != null)
			{
				FinishJob();
			}
			PublishWorkStatus(drinking);
			if (drinking)
			{
				return;
			}
			if (job != null)
			{
				RunJob();
				return;
			}
			if (waypoint < route.Count)
			{
				WalkRoute();
				if (Object.op_Implicit((Object)(object)crossingDoor))
				{
					return;
				}
			}
			if (Time.time >= nextSearch)
			{
				nextSearch = Time.time + 1f;
				StartJob();
			}
		}

		private void UpdateRemotePosition()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: 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_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			Vector3 vec = view.GetZDO().GetVec3(FeetKey, ((Component)this).transform.position);
			if (!remotePositionLoaded)
			{
				remotePositionLoaded = true;
				SetVisualPosition(vec);
			}
			Vector3 val = Vector3.ProjectOnPlane(vec - visual.position, Vector3.up);
			if (((Vector3)(ref val)).sqrMagnitude > 0.01f)
			{
				remoteWalkingUntil = Time.time + 0.15f;
				visual.rotation = Quaternion.RotateTowards(visual.rotation, Quaternion.LookRotation(val), 540f * Time.deltaTime);
			}
			SetVisualPosition(Vector3.MoveTowards(visual.position, vec, MovementSpeed * 1.35f * Time.deltaTime));
			int num;
			if (!(Time.time < remoteWalkingUntil))
			{
				Vector3 val2 = vec - visual.position;
				num = ((((Vector3)(ref val2)).sqrMagnitude > 0.0025f) ? 1 : 0);
			}
			else
			{
				num = 1;
			}
			SetWalking((byte)num != 0);
		}

		private void SetVisualPosition(Vector3 position)
		{
			//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_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_003d: Unknown result type (might be due to invalid IL or missing references)
			visual.position = position;
			Vector3 val = bodyColliderCenter + ((Component)this).transform.InverseTransformPoint(position);
			if (bodyCollider.center != val)
			{
				bodyCollider.center = val;
			}
		}

		private void PublishPosition(bool arrived)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			if ((arrived || Time.time >= nextPositionSync) && Object.op_Implicit((Object)(object)view) && view.IsValid() && view.IsOwner())
			{
				view.GetZDO().Set(FeetKey, feet);
				nextPositionSync = Time.time + 0.2f;
			}
		}

		private void StartJob()
		{
			//IL_00cf: 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)
			RefreshWorkbenches();
			if (cargo != null)
			{
				ResumeCargo();
			}
			else
			{
				if (HandleFloorSweep())
				{
					return;
				}
				bool flag = false;
				bool flag2 = false;
				bool flag3 = false;
				bool flag4 = false;
				foreach (SortingBox item in SortingBox.Loaded.OrderBy((SortingBox box) => box.VisitOrder).ThenBy(delegate(SortingBox box)
				{
					//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_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)
					Vector3 val2 = ((Component)box).transform.position - feet;
					return ((Vector3)(ref val2)).sqrMagnitude;
				}))
				{
					Container container = item.Container;
					if (!Object.op_Implicit((Object)(object)container))
					{
						continue;
					}
					Inventory inventory = container.GetInventory();
					int? num = ((inventory != null) ? new int?(inventory.NrOfItems()) : ((int?)null));
					if (!num.HasValue || num.GetValueOrDefault() <= 0)
					{
						continue;
					}
					flag = true;
					CraftingStation val = FindWorkbench(((Component)item).transform.position);
					if (!Object.op_Implicit((Object)(object)val))
					{
						continue;
					}
					flag2 = true;
					flag3 |= container.IsInUse();
					if (!(Time.time < item.RetryAfter) && CanUse(container))
					{
						if (TryRoute(((Component)container).transform.position, 1.15f, val, ((Component)container).transform))
						{
							item.VisitOrder = ++visitOrder;
							job = new Job
							{
								Source = container,
								Workbench = val
							};
							FollowCandidateRoute();
							return;
						}
						flag4 = true;
						item.RetryAfter = Time.time + 2f;
					}
				}
				if (flag4)
				{
					SetStatus("Waiting for a walkable route to the sorting box.");
				}
				else if (flag3)
				{
					SetStatus("Waiting for the sorting box to close.", WorkStatus.WaitingForBox);
				}
				else if (!flag2)
				{
					SetStatus(flag ? "Kvastur and the sorting box are outside a shared workbench build area." : null, WorkStatus.NoWorkbench);
				}
				if (!flag && sweptLoot)
				{
					sweptLoot = false;
					Speak(SpeechLine.SweepDone);
				}
				TryIdleWalk();
			}
		}

		private static void RefreshWorkbenches()
		{
			if (Time.time >= nextStationRefresh)
			{
				nextStationRefresh = Time.time + 1f;
				stations = Object.FindObjectsByType<CraftingStation>((FindObjectsSortMode)0);
			}
		}

		private CraftingStation FindWorkbench(Vector3 boxPosition, Vector3? destination = null)
		{
			//IL_0041: 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_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_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			CraftingStation result = null;
			float num = float.PositiveInfinity;
			CraftingStation[] array = stations;
			foreach (CraftingStation val in array)
			{
				if (Object.op_Implicit((Object)(object)val) && !(val.m_name != "$piece_workbench") && WithinWorkbench(val, ((Component)this).transform.position) && WithinWorkbench(val, boxPosition) && (!destination.HasValue || (WithinWorkbench(val, destination.Value) && WithinWorkbench(val, feet))))
				{
					Vector3 val2 = ((Component)val).transform.position - boxPosition;
					float sqrMagnitude = ((Vector3)(ref val2)).sqrMagnitude;
					if (sqrMagnitude < num)
					{
						num = sqrMagnitude;
						result = val;
					}
				}
			}
			return result;
		}

		private static void RefreshDeliveryChests()
		{
			if (Time.time >= nextChestRefresh)
			{
				nextChestRefresh = Time.time + 1f;
				chests = Object.FindObjectsByType<Container>((FindObjectsSortMode)0).Where(IsSortingDestination).ToArray();
			}
		}

		private IEnumerable<DeliverySearch> FindDelivery()
		{
			RefreshWorkbenches();
			RefreshDeliveryChests();
			KeyValuePair<Container, (float, Vector3)>[] array = failedDeliveries.Where((KeyValuePair<Container, (float Until, Vector3 Start)> entry) => !Object.op_Implicit((Object)(object)entry.Key) || entry.Value.Until <= Time.time).ToArray();
			foreach (KeyValuePair<Container, (float, Vector3)> keyValuePair in array)
			{
				failedDeliveries.Remove(keyValuePair.Key);
			}
			bool chestFull = false;
			bool reservedCargo = false;
			Vector3 deliveryOrigin = DeliveryOrigin;
			List<(Container Chest, CraftingStation Workbench, ChestCategory Category)> destinations = new List<(Container, CraftingStation, ChestCategory)>();
			Container[] array2 = chests;
			int num;
			for (num = 0; num < array2.Length; num++)
			{
				Container val = array2[num];
				if (!IsSortingDestination(val))
				{
					continue;
				}
				CraftingStation val2 = FindWorkbench(deliveryOrigin, ((Component)val).transform.position);
				if (val2 != null && Object.op_Implicit((Object)(object)val2) && CanUse(val, allowOpen: true))
				{
					ChestCategory chestCategory = ChestCategories.Read(val);
					if (chestCategory != ChestCategory.Ignore)
					{
						destinations.Add((val, val2, chestCategory));
					}
				}
			}
			List<(Container Chest, CraftingStation Workbench, int Priority, float Distance)> candidates = new List<(Container, CraftingStation, int, float)>();
			foreach (ItemData item in cargo.GetAllItems())
			{
				candidates.Clear();
				ChestCategory chestCategory2 = ChestCategories.Classify(item);
				bool flag = HasAssignedRoom(item, chestCategory2);
				reservedCargo = reservedCargo || flag;
				Vector3 val5;
				foreach (var (val3, val4, destination) in destinations)
				{
					if (Object.op_Implicit((Object)(object)val3) && Object.op_Implicit((Object)(object)val4) && ChestCategories.Accepts(destination, chestCategory2, flag))
					{
						Inventory inventory = val3.GetInventory();
						if (!TryFindSlot(inventory, item, out var _, out num, out var hasMatch))
						{
							chestFull = true;
							continue;
						}
						int item2 = ((!hasMatch) ? ((inventory.NrOfItems() == 0) ? 1 : 2) : 0);
						val5 = ((Component)val3).transform.position - feet;
						candidates.Add((val3, val4, item2, ((Vector3)(ref val5)).sqrMagnitude));
					}
				}
				foreach (var (chest, workbench, _, _) in from candidate in candidates
					orderby candidate.Priority, candidate.Distance
					select candidate)
				{
					if (!Object.op_Implicit((Object)(object)chest) || !Object.op_Implicit((Object)(object)workbench) || !CanUse(chest, allowOpen: true))
					{
						continue;
					}
					if (failedDeliveries.TryGetValue(chest, out (float, Vector3) value) && value.Item1 > Time.time)
					{
						val5 = value.Item2 - feet;
						if (((Vector3)(ref val5)).sqrMagnitude < 4f)
						{
							continue;
						}
					}
					foreach (bool item3 in RouteAttempts(((Component)chest).transform.position, 0.85f, workbench, ((Component)chest).transform))
					{
						if (item3)
						{
							job.Destination = chest;
							job.Item = item;
							job.Workbench = workbench;
							FollowCandidateRoute();
							yield return DeliverySearch.Found;
							yield break;
						}
						yield return DeliverySearch.Pending;
						if (!Object.op_Implicit((Object)(object)chest) || !Object.op_Implicit((Object)(object)workbench) || !CanUse(chest, allowOpen: true))
						{
							break;
						}
					}
					if (Object.op_Implicit((Object)(object)chest))
					{
						failedDeliveries[chest] = (Time.time + 8f, feet);
					}
				}
			}
			if (reservedCargo)
			{
				SetStatus("A category chest has room but is unavailable. Kvastur will retry it.", WorkStatus.WaitingForChest);
			}
			yield return chestFull ? DeliverySearch.Full : DeliverySearch.Unavailable;
		}

		private void RunJob()
		{
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: 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_01e1: Unknown result type (might be due to invalid IL or missing references)
			if ((!Object.op_Implicit((Object)(object)job.Source) && !job.DirectPickup && !directFloorCargo) || (!Object.op_Implicit((Object)(object)job.Workbench) && (cargo == null || job.FloorDrop != null)))
			{
				FinishJob();
			}
			else
			{
				if (!WalkRoute())
				{
					return;
				}
				if (job.Returning)
				{
					ReturnCargo();
					return;
				}
				if (job.FloorDrop != null)
				{
					CollectFloorCargo();
					return;
				}
				if (cargo == null)
				{
					if (!CanUse(job.Source) || !CanReach(job.Source) || !WithinWorkbench(job.Workbench, ((Component)job.Source).transform.position) || !WithinWorkbench(job.Workbench, ((Component)this).transform.position) || job.Source.GetInventory().NrOfItems() == 0)
					{
						FinishJob();
						return;
					}
					CollectCargo();
					if (Drinking && cargo.NrOfItems() > 0)
					{
						return;
					}
				}
				else if (Object.op_Implicit((Object)(object)job.Destination) && Object.op_Implicit((Object)(object)job.Workbench) && CanUse(job.Destination, allowOpen: true) && CanReach(job.Destination) && WithinWorkbench(job.Workbench, ((Component)job.Destination).transform.position) && WithinWorkbench(job.Workbench, DeliveryOrigin) && WithinWorkbench(job.Workbench, ((Component)this).transform.position))
				{
					UnloadCargo(job.Destination, matchesOnly: true);
				}
				if (cargo.NrOfItems() == 0)
				{
					if (Object.op_Implicit((Object)(object)job.Destination))
					{
						Speak(SpeechLine.Done);
					}
					ClearCargo();
					FinishJob();
				}
				else if (Object.op_Implicit((Object)(object)job.Destination))
				{
					job.Destination = null;
					job.Item = null;
				}
				else
				{
					PlanCargoDelivery();
				}
			}
		}

		private void CollectCargo()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: 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_00f0: Unknown result type (might be due to invalid IL or missing references)
			RefreshContainerInventory(job.Source);
			Inventory inventory = job.Source.GetInventory();
			bool flag = ((Component)job.Source).GetComponent<SortingBox>().ConsumePlayerDeposit();
			cargo = new Inventory("Kvastur cargo", (Sprite)null, inventory.GetWidth(), inventory.GetHeight());
			SetCargoSource(job.Source);
			Action onChanged = inventory.m_onChanged;
			inventory.m_onChanged = null;
			ItemData val = ((!Boosted) ? ((IEnumerable<ItemData>)inventory.GetAllItems()).FirstOrDefault((Func<ItemData, bool>)IsTastyMead) : null);
			try
			{
				if (val != null)
				{
					inventory.RemoveItem(val, 1);
				}
				ItemData[] array = inventory.GetAllItems().ToArray();
				foreach (ItemData val2 in array)
				{
					Vector2i gridPos = val2.m_gridPos;
					cargo.MoveItemToThis(inventory, val2, val2.m_stack, gridPos.x, gridPos.y);
				}
			}
			finally
			{
				inventory.m_onChanged = onChanged;
				if (cargo.NrOfItems() > 0 || val != null)
				{
					SaveCargo();
					onChanged?.Invoke();
				}
			}
			if (val != null)
			{
				DrinkTastyMead();
			}
			SetStatus(null);
			Plugin.Log.LogInfo((object)$"Kvastur collected {cargo.NrOfItems()} stacks from {((Object)job.Source).name}.");
			if (flag && cargo.NrOfItems() > 0)
			{
				int stoneCount = (from item in cargo.GetAllItems()
					where item.m_shared.m_name == "$item_stone"
					select item).Sum((ItemData item) => item.m_stack);
				Speak(ChoosePickupLine(stoneCount, SpeechRandom.Next(200), SpeechRandom.Next(1000)), perJob: true);
			}
		}

		private void UnloadCargo(Container container, bool matchesOnly)
		{
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			RefreshContainerInventory(container);
			Inventory inventory = container.GetInventory();
			ChestCategory chestCategory = (matchesOnly ? ChestCategories.Read(container) : ChestCategory.None);
			if (matchesOnly)
			{
				RefreshDeliveryChests();
			}
			bool flag = false;
			int num = 0;
			Action onChanged = inventory.m_onChanged;
			inventory.m_onChanged = null;
			try
			{
				ItemData[] array = cargo.GetAllItems().ToArray();
				foreach (ItemData val in array)
				{
					if (matchesOnly)
					{
						ChestCategory chestCategory2 = ChestCategories.Classify(val);
						if (!ChestCategories.Accepts(chestCategory, chestCategory2, chestCategory == ChestCategory.None && HasAssignedRoom(val, chestCategory2)))
						{
							continue;
						}
					}
					int stack = val.m_stack;
					Vector2i slot;
					int amount;
					bool hasMatch;
					while (val.m_stack > 0 && TryFindSlot(inventory, val, out slot, out amount, out hasMatch) && (!matchesOnly || chestCategory != ChestCategory.None || hasMatch || val == job.Item || !HasBetterChest(container, val)) && inventory.MoveItemToThis(cargo, val, amount, slot.x, slot.y))
					{
					}
					int num2 = stack - val.m_stack;
					num += num2;
					if (num2 > 0)
					{
						flag |= val.m_shared.m_food > 0f || IsBone(val);
					}
				}
			}
			finally
			{
				inventory.m_onChanged = onChanged;
				if (num > 0)
				{
					SaveCargo();
					onChanged?.Invoke();
				}
			}
			if (num > 0)
			{
				string arg = (matchesOnly ? "delivered" : (floorCargo ? "deposited" : "returned"));
				Plugin.Log.LogInfo((object)$"Kvastur {arg} {num} items to {((Object)container).name}.");
			}
			if (matchesOnly && flag && inventory.GetAllItems().Any(IsBone) && inventory.GetAllItems().Any((ItemData item) => item.m_shared.m_food > 0f))
			{
				Speak(SpeechLine.BonesAndFood);
			}
		}

		private bool HasAssignedRoom(ItemData item, ChestCategory category)
		{
			//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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Invalid comparison between Unknown and I4
			//IL_0035: 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_0059: Unknown result type (might be due to invalid IL or missing references)
			if (category == ChestCategory.None)
			{
				return false;
			}
			Vector3 deliveryOrigin = DeliveryOrigin;
			Container[] array = chests;
			foreach (Container val in array)
			{
				if (IsSortingDestination(val) && (int)val.m_privacy == 2 && ChestCategories.Read(val) == category && Object.op_Implicit((Object)(object)FindWorkbench(deliveryOrigin, ((Component)val).transform.position)) && HasWorkAccess(((Component)val).transform.position))
				{
					Inventory inventory = val.GetInventory();
					if (inventory != null && TryFindSlot(inventory, item, out var _, out var _, out var _))
					{
						return true;
					}
				}
			}
			return false;
		}

		private bool HasBetterChest(Container current, ItemData item)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			Container[] array = chests;
			foreach (Container val in array)
			{
				if ((Object)(object)val != (Object)(object)current && IsSortingDestination(val) && ChestCategories.Read(val) == ChestCategory.None && CanUse(val, allowOpen: true) && Object.op_Implicit((Object)(object)FindWorkbench(DeliveryOrigin, ((Component)val).transform.position)) && TryFindSlot(val.GetInventory(), item, out var _, out var _, out var hasMatch) && (hasMatch || val.GetInventory().NrOfItems() == 0))
				{
					return true;
				}
			}
			return false;
		}

		private void PlanCargoDelivery()
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			if (deliverySearch == null)
			{
				deliverySearch = FindDelivery().GetEnumerator();
			}
			if (!deliverySearch.MoveNext() || deliverySearch.Current == DeliverySearch.Pending)
			{
				return;
			}
			DeliverySearch current = deliverySearch.Current;
			deliverySearch.Dispose();
			deliverySearch = null;
			if (current == DeliverySearch.Found)
			{
				return;
			}
			if (!Object.op_Implicit((Object)(object)job.Source))
			{
				if (DropCargoAt(feet))
				{
					sweepingFloor = false;
					nextFloorSearch = Time.time + 8f;
					SetStatus("No reachable chest could take the remaining cargo. Kvastur put it on the floor.", WorkStatus.WaitingForChest);
				}
				FinishJob();
			}
			else if (PlanReturn() && current == DeliverySearch.Full)
			{
				Speak(SpeechLine.Full);
			}
		}

		private bool PlanReturn()
		{
			//IL_0011: 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_0040: Unknown result type (might be due to invalid IL or missing references)
			CraftingStation val = FindWorkbench(((Component)job.Source).transform.position, feet);
			if (Object.op_Implicit((Object)(object)val) && TryRoute(((Component)job.Source).transform.position, 1.15f, val, ((Component)job.Source).transform))
			{
				job.Workbench = val;
				job.Returning = true;
				FollowCandidateRoute();
				return true;
			}
			SetStatus("Waiting for a walkable route back to the sorting box with remaining cargo.");
			FinishJob();
			return false;
		}

		private void ReturnCargo()
		{
			//IL_002c: 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_0123: Unknown result type (might be due to invalid IL or missing references)
			if (Time.time < nextSearch)
			{
				return;
			}
			if (!Object.op_Implicit((Object)(object)job.Workbench) || !WithinWorkbench(job.Workbench, feet) || !WithinWorkbench(job.Workbench, ((Component)job.Source).transform.position) || !CanReach(job.Source))
			{
				FinishJob();
				return;
			}
			if (CanUse(job.Source))
			{
				UnloadCargo(job.Source, matchesOnly: false);
				if (cargo.NrOfItems() == 0)
				{
					((Component)job.Source).GetComponent<SortingBox>().RetryAfter = (floorCargo ? 0f : (Time.time + 8f));
					sweepingFloor |= floorCargo;
					sweptLoot |= floorCargo;
					SetStatus(null);
					ClearCargo();
					FinishJob();
					return;
				}
				if (floorCargo && DropCargoAt(feet))
				{
					sweepingFloor = false;
					nextFloorSearch = Time.time + 8f;
					SetStatus("The sorting box filled up during pickup. Kvastur put the excess back on the floor.");
					FinishJob();
					return;
				}
			}
			SetStatus("Waiting for room in the original sorting box to return cargo.", WorkStatus.WaitingForBox);
			nextSearch = Time.time + 1f;
		}

		private void ResumeCargo()
		{
			//IL_00a6: 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)
			if (directFloorCargo)
			{
				if (!Boosted || !HandleFloorSweep())
				{
					job = new Job();
					PlanCargoDelivery();
				}
				return;
			}
			Container val = SortingBox.Loaded.Select((SortingBox box) => box.Container).FirstOrDefault((Func<Container, bool>)delegate(Container container)
			{
				if (Object.op_Implicit((Object)(object)container))
				{
					ZNetView component = ((Component)container).GetComponent<ZNetView>();
					if (component != null && component.IsValid())
					{
						return IsCargoSource(component.GetZDO());
					}
				}
				return false;
			});
			if (!Object.op_Implicit((Object)(object)val))
			{
				if (IsCargoSourceMissing() && DropCargoAt(DropPosition))
				{
					SetStatus("The original sorting box is missing. Kvastur put the remaining cargo on the floor.");
				}
				return;
			}
			job = new Job
			{
				Source = val,
				Workbench = FindWorkbench(((Component)val).transform.position)
			};
			if (floorCargo)
			{
				PlanReturn();
			}
			else
			{
				PlanCargoDelivery();
			}
		}

		private void SetCargoSource(Container source)
		{
			//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_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_0025: Unknown result type (might be due to invalid IL or missing references)
			confirmedMissingSource = ZDOID.None;
			ZDO zDO = ((Component)source).GetComponent<ZNetView>().GetZDO();
			cargoSource = zDO.m_uid;
			cargoSourcePosition = zDO.GetPosition();
		}

		private bool IsCargoSource(ZDO data)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: 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_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_0045: Unknown result type (might be due to invalid IL or missing references)
			if (data != null && data.GetPrefab() == SortingBoxPrefab)
			{
				Vector3? val = cargoSourcePosition;
				if (val.HasValue)
				{
					Vector3 valueOrDefault = val.GetValueOrDefault();
					Vector3 position = data.GetPosition();
					return ((Vector3)(ref position)).Equals(valueOrDefault);
				}
				return data.m_uid == cargoSource;
			}
			return false;
		}

		private bool CargoSourceExists()
		{
			//IL_0007: 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_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: 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)
			if (IsCargoSource(ZDOMan.instance.GetZDO(cargoSource)))
			{
				return true;
			}
			Vector3? val = cargoSourcePosition;
			if (val.HasValue)
			{
				Vector3 valueOrDefault = val.GetValueOrDefault();
				cargoSourceCandidates.Clear();
				ZDOMan.instance.FindSectorObjects(ZoneSystem.GetZone(valueOrDefault), default(SimulationDistance), cargoSourceCandidates, (List<ZDO>)null);
				bool result = cargoSourceCandidates.Exists(IsCargoSource);
				cargoSourceCandidates.Clear();
				return result;
			}
			return false;
		}

		private void LoadCargo()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			if (cargoLoaded)
			{
				return;
			}
			cargoLoaded = true;
			cargo = null;
			cargoSourcePosition = null;
			floorCargo = false;
			directFloorCargo = false;
			string text = view.GetZDO().GetString("KvasturSorter.Cargo", "");
			if (text.Length > 0)
			{
				ZPackage val = new ZPackage(text);
				cargoSource = val.ReadZDOID();
				cargo = new Inventory("Kvastur cargo", (Sprite)null, val.ReadInt(), val.ReadInt());
				cargo.Load(val);
				floorCargo = val.GetPos() < val.Size() && val.ReadBool();
				if (val.Size() - val.GetPos() >= 12)
				{
					cargoSourcePosition = val.ReadVector3();
					directFloorCargo = val.GetPos() < val.Size() && val.ReadBool();
				}
			}
		}

		private void SaveCargo()
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)view) || !view.IsValid() || !view.IsOwner())
			{
				return;
			}
			if (cargo == null || cargo.NrOfItems() == 0)
			{
				view.GetZDO().Set("KvasturSorter.Cargo", "");
				return;
			}
			ZPackage val = new ZPackage();
			val.Write(cargoSource);
			val.Write(cargo.GetWidth());
			val.Write(cargo.GetHeight());
			cargo.Save(val);
			val.Write(floorCargo);
			Vector3? val2 = cargoSourcePosition;
			if (val2.HasValue)
			{
				Vector3 valueOrDefault = val2.GetValueOrDefault();
				val.Write(valueOrDefault);
				val.Write(directFloorCargo);
			}
			view.GetZDO().Set("KvasturSorter.Cargo", val.GetBase64());
		}

		private void ClearCargo()
		{
			cargo = null;
			floorCargo = false;
			directFloorCargo = false;
			SaveCargo();
		}

		private void DropCargo()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)view) && view.IsValid() && view.IsOwner())
			{
				LoadCargo();
				DropCargoAt(DropPosition);
			}
		}

		private bool DropCargoAt(Vector3 position)
		{
			//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_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			if (cargo == null)
			{
				return true;
			}
			ItemData[] array = cargo.GetAllItems().ToArray();
			ZNetView val3 = default(ZNetView);
			foreach (ItemData val in array)
			{
				ItemDrop val2 = ItemDrop.DropItem(val, val.m_stack, position + Vector3.up * 0.5f, Quaternion.identity);
				if (Object.op_Implicit((Object)(object)val2) && ((Component)val2).TryGetComponent<ZNetView>(ref val3) && val3.IsValid())
				{
					cargo.RemoveItem(val);
				}
				else if (Object.op_Implicit((Object)(object)val2))
				{
					Object.Destroy((Object)(object)((Component)val2).gameObject);
				}
			}
			if (cargo.NrOfItems() == 0)
			{
				ClearCargo();
				return true;
			}
			SaveCargo();
			return false;
		}

		private void FinishJob()
		{
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			deliverySearch?.Dispose();
			deliverySearch = null;
			if (Object.op_Implicit((Object)(object)job?.FloorDrop) && Object.op_Implicit((Object)(object)job.Workbench))
			{
				MarkFloorRouteBlocked(job.FloorDrop, job.Workbench);
			}
			job = null;
			route.Clear();
			waypoint = 0;
			SetVisualPosition(feet);
			SetWalking(moving: false);
			ReleaseDoor();
			PublishPosition(arrived: true);
			nextSearch = Time.time + 0.3f;
		}

		private bool TryRoute(Vector3 target, float standOff, CraftingStation station, Transform interactionTarget = null, bool allowDoors = true, bool floorPickup = false, bool requireShelter = false)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			return RouteAttempts(target, standOff, station, interactionTarget, allowDoors, floorPickup, requireShelter).Any((bool found) => found);
		}

		private IEnumerable<bool> RouteAttempts(Vector3 target, float standOff, CraftingStation station, Transform interactionTarget, bool allowDoors = true, bool floorPickup = false, bool requireShelter = false)
		{
			//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)
			Vector3 val = Vector3.ProjectOnPlane(feet - target, Vector3.up);
			Vector3 side = ((Vector3)(ref val)).normalized;
			if (side == Vector3.zero)
			{
				side = Vector3.forward;
			}
			Container val2 = default(Container);
			bool container = Object.op_Implicit((Object)(object)interactionTarget) && ((Component)interactionTarget).TryGetComponent<Container>(ref val2);
			int approaches = ((container && target.y - feet.y > 0.5f) ? 8 : ((!(standOff > 0f)) ? 1 : 4));
			int passes = ((!allowDoors) ? 1 : 2);
			for (int pass = 0; pass < passes; pass++)
			{
				Vector3 direction = side;
				for (int i = 0; i < approaches; i++)
				{
					bool flag = i >= 4;
					if (i == 4)
					{
						direction = interactionTarget.forward;
					}
					Vector3 to = target + direction * (standOff + (flag ? 0.55f : 0f));
					direction = new Vector3(direction.z, 0f, 0f - direction.x);
					if (flag)
					{
						to.y = Mathf.Max(feet.y, target.y - 2.55f);
					}
					bool walkable = false;
					foreach (bool item in DoorRoutes.FindSteps(feet, to, station, candidateRoute, pass != 0, HasWorkAccess))
					{
						if (item)
						{
							walkable = true;
							break;
						}
						yield return false;
					}
					if (!walkable)
					{
						yield return false;
						continue;
					}
					Vector3 point = candidateRoute[candidateRoute.Count - 1].Point;
					if ((!floorPickup || WithinPickupDistance(point, target)) && (!requireShelter || UnderRoof(point)) && (!Object.op_Implicit((Object)(object)interactionTarget) || (container ? CanReachContainer(point, interactionTarget) : (!Object.op_Implicit((Object)(object)DoorRoutes.FindObstacle(point, target, interactionTarget))))))
					{
						yield return true;
						yield break;
					}
					yield return false;
				}
			}
		}

		private bool CanReach(Container container)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			return CanReachContainer(feet, ((Component)container).transform);
		}

		private static bool CanReachContainer(Vector3 from, Transform target)
		{
			//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_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: 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_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: 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_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: 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_006b: 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_007a: Unknown result type (might be due to invalid IL or missing references)
			Vector3 position = target.position;
			if (!Object.op_Implicit((Object)(object)DoorRoutes.FindObstacle(from, position, target)))
			{
				return true;
			}
			float num = position.y - from.y;
			if (!(num <= 0.5f) && !(num > 2.55f))
			{
				Vector3 val = Vector3.ProjectOnPlane(position - from, Vector3.up);
				if (!(((Vector3)(ref val)).sqrMagnitude > 4f))
				{
					Vector3 val2 = from + Vector3.up * num;
					if (!Object.op_Implicit((Object)(object)DoorRoutes.FindObstacle(from, val2, target)))
					{
						return !Object.op_Implicit((Object)(object)DoorRoutes.FindObstacle(val2, position, target));
					}
					return false;
				}
			}
			return false;
		}

		private void FollowCandidateRoute()
		{
			//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)
			if (UnderRoof(feet))
			{
				lastShelter = feet;
				shelterProbe = 0;
			}
			ReleaseDoor();
			route.Clear();
			route.AddRange(candidateRoute);
			waypoint = 0;
		}

		private bool WalkRoute()
		{
			//IL_0067: 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_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: 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_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			if (waypoint >= route.Count)
			{
				return true;
			}
			DoorRoutes.Step step = route[waypoint];
			if (Object.op_Implicit((Object)(object)step.Door) && !PreparePassage(step))
			{
				SetWalking(moving: false);
				return false;
			}
			Vector3 val = (Object.op_Implicit((Object)(object)step.Door) ? doorPassage[doorWaypoint] : step.Point);
			Vector3 val2 = Vector3.ProjectOnPlane(val - feet, Vector3.up);
			if (((Vector3)(ref val2)).sqrMagnitude > 0.01f)
			{
				visual.rotation = Quaternion.RotateTowards(visual.rotation, Quaternion.LookRotation(val2), 540f * Time.deltaTime);
			}
			Vector3 val3 = Vector3.MoveTowards(feet, val, MovementSpeed * Time.deltaTime);
			Collider val4 = DoorRoutes.FindObstacle(feet, val3);
			if (Object.op_Implicit((Object)(object)val4))
			{
				if (Object.op_Implicit((Object)(object)job?.Destination))
				{
					failedDeliveries[job.Destination] = (Time.time + 8f, feet);
				}
				if (Object.op_Implicit((Object)(object)job?.Source))
				{
					((Component)job.Source).GetComponent<SortingBox>().RetryAfter = Time.time + 1f;
				}
				Piece componentInParent = ((Component)val4).GetComponentInParent<Piece>();
				string text = (Object.op_Implicit((Object)(object)componentInParent) ? ((Object)componentInParent).name : ((Object)val4).name);
				SetStatus("Route blocked by " + text + "; replanning.");
				FinishJob();
				return false;
			}
			Vector3 val5 = val3 - feet;
			bool flag = ((Vector3)(ref val5)).sqrMagnitude > 1E-06f;
			feet = val3;
			if (feet == val && (!Object.op_Implicit((Object)(object)step.Door) || ++doorWaypoint >= doorPassage.Count))
			{
				waypoint++;
				ReleaseDoor();
			}
			bool flag2 = waypoint >= route.Count;
			SetWalking(flag && !flag2);
			SetVisualPosition(feet);
			PublishPosition(flag2);
			return flag2;
		}

		private bool PreparePassage(DoorRoutes.Step step)
		{
			//IL_004a: 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_008d: 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_014c: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)crossingDoor))
			{
				crossingDoor = step.Door;
				doorDeadline = Time.time + 4f;
			}
			if (doorPassage.Count > 0)
			{
				return true;
			}
			if (!doorAcquired)
			{
				doorAcquired = DoorRoutes.Acquire(crossingDoor, feet, HasWorkAccess, out doorLease);
			}
			if (doorAcquired && DoorRoutes.IsOpen(crossingDoor) && DoorRoutes.TryPassage(crossingDoor, feet, step.Point, doorPassage))
			{
				return true;
			}
			if (Time.time >= doorDeadline)
			{
				if (Object.op_Implicit((Object)(object)job?.Destination))
				{
					failedDeliveries[job.Destination] = (Time.time + 8f, feet);
				}
				if (job != null && Object.op_Implicit((Object)(object)job.Source))
				{
					((Component)job.Source).GetComponent<SortingBox>().RetryAfter = Time.time + 4f;
				}
				SetStatus($"Could not pass {((Object)crossingDoor).name} at {((Component)crossingDoor).transform.position}: " + (doorAcquired ? "the doorway did not clear." : "the door could not be opened."));
				Speak(SpeechLine.DoorBlocked, perJob: true);
				FinishJob();
			}
			return false;
		}

		private void OnDisable()
		{
			ActiveHelpers.Remove(this);
			ReleaseDoor();
		}

		private void ReleaseDoor()
		{
			if (doorLease)
			{
				DoorRoutes.Release(crossingDoor);
			}
			crossingDoor = null;
			doorAcquired = false;
			doorLease = false;
			doorPassage.Clear();
			doorWaypoint = 0;
		}

		private void SetStatus(string status, WorkStatus hint = WorkStatus.RouteBlocked)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			if (status != lastStatus && status != null)
			{
				Plugin.Log.LogInfo((object)$"Kvastur at {feet}: {status}");
			}
			lastStatus = status;
			workHint = ((status != null) ? hint : WorkStatus.Idle);
			workHintUntil = Time.time + 4f;
		}

		internal static bool WithinWorkbench(CraftingStation station, Vector3 point)
		{
			//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_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: 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)
			Vector3 position = ((Component)station).transform.position;
			point.y = position.y;
			float stationBuildRange = station.GetStationBuildRange();
			Vector3 val = point - position;
			return ((Vector3)(ref val)).sqrMagnitude < stationBuildRange * stationBuildRange;
		}

		private bool CanUse(Container container, bool allowOpen = false)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Invalid comparison between Unknown and I4
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)container) || (!allowOpen && container.IsInUse()))
			{
				return false;
			}
			ZNetView component = ((Component)container).GetComponent<ZNetView>();
			if ((int)container.m_privacy == 2 && Object.op_Implicit((Object)(object)component) && component.IsValid() && component.IsOwner() && container.GetInventory() != null)
			{
				return HasWorkAccess(((Component)container).transform.position);
			}
			return false;
		}

		internal static bool IsSortingDestination(Container container)
		{
			ZNetView val = default(ZNetView);
			if (Object.op_Implicit((Object)(object)container) && ((Component)container).TryGetComponent<ZNetView>(ref val) && val.IsValid())
			{
				return ChestPrefabs.Contains(val.GetZDO().GetPrefab());
			}
			return false;
		}

		private static bool TryFindSlot(Inventory inventory, ItemData item, out Vector2i slot, out int amount, out bool hasMatch)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			slot = default(Vector2i);
			amount = 0;
			hasMatch = false;
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				if (!((Object)(object)allItem.m_dropPrefab != (Object)(object)item.m_dropPrefab) && !(allItem.m_shared.m_name != item.m_shared.m_name))
				{
					hasMatch = true;
					int spaceLeftInStack = allItem.GetSpaceLeftInStack();
					if (spaceLeftInStack > 0 && CanMerge(allItem, item))
					{
						slot = allItem.m_gridPos;
						amount = Math.Min(spaceLeftInStack, item.m_stack);
						return amount > 0;
					}
				}
			}
			if (inventory.GetEmptySlots() == 0)
			{
				return false;
			}
			for (int i = 0; i < inventory.GetHeight(); i++)
			{
				for (int j = 0; j < inventory.GetWidth(); j++)
				{
					if (inventory.GetItemAt(j, i) == null)
					{
						slot = new Vector2i(j, i);
						amount = Math.Min(item.m_stack, item.m_shared.m_maxStackSize);
						return amount > 0;
					}
				}
			}
			return false;
		}

		private static bool CanMerge(ItemData existing, ItemData item)
		{
			if ((Object)(object)existing.m_dropPrefab != (Object)(object)item.m_dropPrefab || !existing.IsSameType(item) || existing.m_variant != item.m_variant || existing.m_worldLevel != item.m_worldLevel || existing.m_cheated != item.m_cheated || existing.m_durability != item.m_durability || existing.m_crafterID != item.m_crafterID || existing.m_crafterName != item.m_crafterName || existing.m_customData.Count != item.m_customData.Count)
			{
				return false;
			}
			foreach (KeyValuePair<string, string> customDatum in item.m_customData)
			{
				if (!existing.m_customData.TryGetValue(customDatum.Key, out var value) || value != customDatum.Value)
				{
					return false;
				}
			}
			return true;
		}

		private static bool IsTastyMead(ItemData item)
		{
			if (Object.op_Implicit((Object)(object)item.m_dropPrefab))
			{
				return ((Object)item.m_dropPrefab).name == "MeadTasty";
			}
			return false;
		}

		private void UpdateDrinkVisual(bool drinking)
		{
			//IL_0169: 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_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: 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)
			if (!drinking)
			{
				if (Object.op_Implicit((Object)(object)drinkProp))
				{
					Object.Destroy((Object)(object)((Component)drinkProp).gameObject);
					drinkProp = null;
				}
				return;
			}
			if (!Object.op_Implicit((Object)(object)drinkProp))
			{
				GameObject prefab = PrefabManager.Instance.GetPrefab("MeadTasty");
				MeshFilter val = (Object.op_Implicit((Object)(object)prefab) ? prefab.GetComponentInChildren<MeshFilter>(true) : null);
				MeshRenderer val2 = default(MeshRenderer);
				if (!Object.op_Implicit((Object)(object)val) || !Object.op_Implicit((Object)(object)val.sharedMesh) || !((Component)val).TryGetComponent<MeshRenderer>(ref val2))
				{
					return;
				}
				GameObject val3 = new GameObject("KvasturTastyMead");
				drinkProp = val3.transform;
				drinkProp.SetParent(visual, false);
				val3.AddComponent<MeshFilter>().sharedMesh = val.sharedMesh;
				((Renderer)val3.AddComponent<MeshRenderer>()).sharedMaterials = ((Renderer)val2).sharedMaterials;
				Bounds bounds = val.sharedMesh.bounds;
				Vector3 size = ((Bounds)(ref bounds)).size;
				drinkProp.localScale = Vector3.one * (0.22f / Mathf.Max(0.01f, Mathf.Max(size.x, Mathf.Max(size.y, size.z))));
			}
			float num = Mathf.Clamp01(Mathf.Sin(Mathf.Clamp01((float)(3000000000u - BoostRemaining) / 8000000f) * MathF.PI) * 1.35f);
			drinkProp.localPosition = Vector3.Lerp(new Vector3(0.25f, 0.68f, 0.25f), new Vector3(0.03f, 1.03f, 0.27f), num);
			drinkProp.localRotation = Quaternion.Euler(-60f * num, 0f, 0f);
		}

		private void DrinkTastyMead()
		{
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			view.GetZDO().Set(BoostUntilKey, ZNet.instance.GetTime().Ticks + 3000000000u);
			SetWalking(moving: false);
			GameObject prefab = PrefabManager.Instance.GetPrefab("MeadHasty");
			ItemDrop val = default(ItemDrop);
			if (Object.op_Implicit((Object)(object)prefab) && prefab.TryGetComponent<ItemDrop>(ref val))
			{
				StatusEffect consumeStatusEffect = val.m_itemData.m_shared.m_consumeStatusEffect;
				if (consumeStatusEffect != null && Object.op_Implicit((Object)(object)consumeStatusEffect))
				{
					GameObject[] array = consumeStatusEffect.m_startEffects.Create(feet, visual.rotation, visual, 1f, -1, default(ZDOID));
					TimedDestruction val3 = default(TimedDestruction);
					foreach (GameObject val2 in array)
					{
						if (!val2.TryGetComponent<TimedDestruction>(ref val3))
						{
							val3 = val2.AddComponent<TimedDestruction>();
						}
						if (!val3.m_triggerOnAwake || val3.m_timeout > 3f)
						{
							((MonoBehaviour)val3).CancelInvoke();
							val3.Trigger(3f);
						}
					}
				}
			}
			Plugin.Log.LogInfo((object)$"Kvastur drank tasty mead and will work faster for {5} minutes.");
		}

		private static int CargoRoom(Inventory inventory, ItemData item)
		{
			return inventory.GetEmptySlots() * item.m_shared.m_maxStackSize + (from stack in inventory.GetAllItems()
				where CanMerge(stack, item)
				select stack).Sum((ItemData stack) => stack.GetSpaceLeftInStack());
		}

		private void AddDirectFloorCargo(ItemData item)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: 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)
			if (cargo == null)
			{
				cargo = new Inventory("Kvastur cargo", (Sprite)null, 4, 4);
				cargoSource = ZDOID.None;
				cargoSourcePosition = ((Component)this).transform.position;
			}
			Vector2i slot;
			int amount;
			bool hasMatch;
			while (item.m_stack > 0 && TryFindSlot(cargo, item, out slot, out amount, out hasMatch))
			{
				ItemData itemAt = cargo.GetItemAt(slot.x, slot.y);
				if (itemAt != null)
				{
					itemAt.m_stack += amount;
				}
				else
				{
					ItemData val = item.Clone();
					val.m_gridPos = slot;
					val.m_stack = amount;
					cargo.GetAllItems().Add(val);
				}
				item.m_stack -= amount;
			}
			floorCargo = true;
			directFloorCargo = true;
		}

		internal static void InstallHover(Harmony harmony)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "FindHoverObject", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Kvastur), "FindNearbyHover", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private void OnEnable()
		{
			ActiveHelpers.Add(this);
		}

		private static void FindNearbyHover(Player __instance, ref GameObject hover, Character hoverCreature)
		{
			//IL_0051: 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_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer || ((Character)__instance).InPlaceMode() || Object.op_Implicit((Object)(object)hoverCreature) || (Object.op_Implicit((Object)(object)hover) && (hover.GetComponentInParent<Hoverable>() != null || hover.GetComponentInParent<Interactable>() != null)) || !Object.op_Implicit((Object)(object)GameCamera.instance))
			{
				return;
			}
			Transform transform = ((Component)GameCamera.instance).transform;
			Vector3 eyePoint = ((Character)__instance).GetEyePoint();
			float num = 0.7225f;
			RaycastHit val2 = default(RaycastHit);
			foreach (Kvastur activeHelper in ActiveHelpers)
			{
				if (Object.op_Implicit((Object)(object)activeHelper) && Object.op_Implicit((Object)(object)activeHelper.view) && activeHelper.view.IsValid())
				{
					Vector3 val = activeHelper.DropPosition + Vector3.up * 0.65f;
					float num2 = AimDistanceSquared(transform.position, transform.forward, val);
					if (!(num2 >= num) && WithinControlRange(eyePoint, val) && (!Physics.Linecast(eyePoint, val, ref val2, SightLayers, (QueryTriggerInteraction)1) || !((Object)(object)((Component)((RaycastHit)(ref val2)).collider).GetComponentInParent<Kvastur>() != (Object)(object)activeHelper)))
					{
						num = num2;
						hover = ((Component)activeHelper).gameObject;
					}
				}
			}
		}

		private static float AimDistanceSquared(Vector3 origin, Vector3 direction, Vector3 point)
		{
			//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_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = point - origin;
			float num = Vector3.Dot(val, direction);
			if (!(num < 0f))
			{
				Vector3 val2 = val - direction * num;
				return ((Vector3)(ref val2)).sqrMagnitude;
			}
			return float.PositiveInfinity;
		}

		private static bool WithinControlRange(Vector3 origin, Vector3 target)
		{
			//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)
			Vector3 val = origin - target;
			return ((Vector3)(ref val)).sqrMagnitude <= 25f;
		}

		public string GetHoverName()
		{
			return Localization.instance.Localize("$novakvastursorter_helper");
		}

		public float GetHoverOffset()
		{
			return 0f;
		}

		public string GetHoverText()
		{
			Localization instance = Localization.instance;
			string selectedLanguage = instance.GetSelectedLanguage();
			string[] array = GetWorkLabels(selectedLanguage);
			int num = ((Object.op_Implicit((Object)(object)view) && view.IsValid()) ? view.GetZDO().GetInt(WorkStatusKey, 0) : 0);
			int num2 = Mathf.Clamp(num % 16, 0, 10);
			ChestCategory chestCategory = ChestCategories.Decode(num / 16);
			string text = array[num2];
			if (num2 == 3 && chestCategory != ChestCategory.None)
			{
				string text2 = text;
				IReadOnlyDictionary<string, string> readOnlyDictionary = CategoryTranslations.ForLanguage(selectedLanguage);
				int num3 = (int)chestCategory;
				text = text2 + " · " + readOnlyDictionary["novakvastursorter_category_item_" + num3];
			}
			bool floorPickupEnabled = FloorPickupEnabled;
			string text3 = GetHoverName() + "\n" + text;
			if (!CanToggleFloorPickup(Player.m_localPlayer))
			{
				return text3;
			}
			return text3 + instance.Localize("\n[<color=yellow><b>$KEY_Use</b></color>] ") + array[floorPickupEnabled ? 11 : 12];
		}

		public bool Interact(Humanoid user, bool hold, bool alt)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			if (!hold)
			{
				Player val = (Player)(object)((user is Player) ? user : null);
				if (val != null && CanToggleFloorPickup(val))
				{
					view.InvokeRPC("NovaKvasturSorter_FloorPickup", new object[2]
					{
						((Character)val).GetZDOID(),
						!FloorPickupEnabled
					});
					return true;
				}
			}
			return false;
		}

		public bool UseItem(Humanoid user, ItemData item)
		{
			return false;
		}

		private bool CanToggleFloorPickup(Player player)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)player) && Object.op_Implicit((Object)(object)view) && view.IsValid() && WithinControlRange(((Character)player).GetEyePoint(), DropPosition + Vector3.up * 0.65f))
			{
				return HasControlAccess(player.GetPlayerID());
			}
			return false;
		}

		private bool HasControlAccess(long playerId)
		{
			//IL_0009: 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)
			if (playerId != 0L && WardAccess.Check(((Component)this).transform.position, playerId))
			{
				return WardAccess.Check(DropPosition, playerId);
			}
			return false;
		}

		private void RegisterFloorToggle()
		{
			if (Object.op_Implicit((Object)(object)view) && view.IsValid())
			{
				view.Register<ZDOID, bool>("NovaKvasturSorter_FloorPickup", (Action<long, ZDOID, bool>)ApplyFloorToggle);
			}
		}

		private void ApplyFloorToggle(long sender, ZDOID actorId, bool enabled)
		{
			//IL_000c: 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_005a: Unknown result type (might be due to invalid IL or missing references)
			ZDOMan instance = ZDOMan.instance;
			ZDO val = ((instance != null) ? instance.GetZDO(actorId) : null);
			if (Object.op_Implicit((Object)(object)view) && view.IsValid() && view.IsOwner() && val != null && val.GetPrefab() == PlayerPrefab && val.GetOwner() == sender && WithinControlRange(val.GetPosition(), DropPosition))
			{
				long playerId = val.GetLong(ZDOVars.s_playerID, 0L);
				if (HasControlAccess(playerId))
				{
					view.GetZDO().Set(FloorDisabledKey, !enabled);
				}
			}
		}

		private void PublishWorkStatus(bool drinking)
		{
			WorkStatus workStatus = GetWorkStatus(drinking);
			int num = (int)((workStatus == WorkStatus.Delivering) ? ChestCategories.Read(job.Destination) : ChestCategory.None);
			int num2 = (int)(workStatus + 16 * num);
			ZDO zDO = view.GetZDO();
			if (zDO.GetInt(WorkStatusKey, 0) != num2)
			{
				zDO.Set(WorkStatusKey, num2, false);
			}
		}

		private WorkStatus GetWorkStatus(bool drinking)
		{
			if (drinking)
			{
				return WorkStatus.Drinking;
			}
			if (job?.FloorDrop != null)
			{
				return WorkStatus.Floor;
			}
			if (Object.op_Implicit((Object)(object)job?.Destination))
			{
				return WorkStatus.Delivering;
			}
			if (Time.time < workHintUntil && workHint != WorkStatus.Idle)
			{
				return workHint;
			}
			Job obj = job;
			if (obj != null && obj.Returning)
			{
				return WorkStatus.Returning;
			}
			if (cargo != null)
			{
				return WorkStatus.Planning;
			}
			if (!Object.op_Implicit((Object)(object)job?.Source))
			{
				return WorkStatus.Idle;
			}
			return WorkStatus.Box;
		}

		private static string[] GetWorkLabels(string language)
		{
			if (workLabels == null)
			{
				workLabels = new Dictionary<string, string[]>();
				using StreamReader streamReader = new StreamReader(typeof(Kvastur).Assembly.GetManifestResourceStream("NovaKvasturSorter.HelperStatus.txt"));
				while (true)
				{
					string text = streamReader.ReadLine();
					if (text != null)
					{
						string[] array = text.Split('|');
						if (array.Length == 14)
						{
							workLabels.Add(array[0], array[1..]);
						}
						continue;
					}
					break;
				}
			}
			if (!workLabels.TryGetValue(language, out var value))
			{
				return workLabels["English"];
			}
			return value;
		}

		private void SetWalking(bool moving)
		{
			float num = ((moving && Boosted) ? 1.8f : 1f);
			if (animator.speed != num)
			{
				animator.speed = num;
			}
			if (moving != walking)
			{
				walking = moving;
				animator.SetFloat(ForwardSpeed, moving ? 2.5f : 0f);
			}
		}

		private void TryIdleWalk()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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_0097: 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_00af: 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_00d1: 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_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_00ee: 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_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: 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_0120: Unknown result type (might be due to invalid IL or missing references)
			if (waypoint < route.Count)
			{
				return;
			}
			CraftingStation val = FindWorkbench(((Component)this).transform.position, feet);
			if (!Object.op_Implicit((Object)(object)val))
			{
				return;
			}
			if (!UnderRoof(feet))
			{
				SeekShelter(val);
				return;
			}
			lastShelter = feet;
			shelterProbe = 0;
			if (Time.time < nextIdleWalk)
			{
				return;
			}
			nextIdleWalk = Time.time + Random.Range(8f, 15f);
			Vector3 val3 = default(Vector3);
			for (int i = 0; i < 6; i++)
			{
				Vector2 val2 = Random.insideUnitCircle * 5f;
				((Vector3)(ref val3))..ctor(((Component)this).transform.position.x + val2.x, feet.y, ((Component)this).transform.position.z + val2.y);
				Vector3 val4 = val3 - feet;
				if (!(((Vector3)(ref val4)).sqrMagnitude < 2.25f) && UnderRoof(val3) && TryRoute(val3, 0f, val, null, allowDoors: false) && IndoorRoute(feet))
				{
					FollowCandidateRoute();
					return;
				}
			}
			if (Object.op_Implicit((Object)(object)Pathfinding.instance))
			{
				LocalNavigation.RequestMesh(feet, feet);
			}
		}

		private void SeekShelter(CraftingStation station)
		{
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: 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_0051: 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_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			if (Time.time < nextShelterSearch)
			{
				return;
			}
			for (int i = 0; i < 35; i++)
			{
				int num = shelterProbe;
				shelterProbe = (shelterProbe + 1) % 35;
				Vector3 val;
				if (num < 3)
				{
					val = (Vector3)(num switch
					{
						0 => ((??)lastShelter) ?? ((Component)this).transform.position, 
						1 => ((Component)this).transform.position, 
						_ => ((Component)station).transform.position, 
					});
				}
				else
				{
					int num2 = num - 3;
					float num3 = (float)(num2 % 8) * (MathF.PI / 4f);
					float num4 = (float)(num2 / 8 + 1) * 0.2f * station.GetStationBuildRange();
					val = ((Component)station).transform.position + new Vector3(Mathf.Cos(num3), 0f, Mathf.Sin(num3)) * num4;
				}
				if (WithinWorkbench(station, val) && UnderRoof(val))
				{
					nextShelterSearch = Time.time + 2.5f;
					if (TryRoute(val, 0f, station, null, allowDoors: true, floorPickup: false, requireShelter: true))
					{
						FollowCandidateRoute();
					}
					return;
				}
			}
			nextShelterSearch = Time.time + 8f;
		}

		private bool IndoorRoute(Vector3 start)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_0041: 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_0057: Unknown result type (might be due to invalid IL or missing references)
			foreach (DoorRoutes.Step item in candidateRoute)
			{
				if (Object.op_Implicit((Object)(object)item.Door) || !UnderRoof(item.Point) || !UnderRoof((start + item.Point) * 0.5f))
				{
					return false;
				}
				start = item.Point;
			}
			return true;
		}

		private static bool UnderRoof(Vector3 position)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			return Physics.Raycast(position + Vector3.up * 0.6f, Vector3.up, 5f, RoofLayers, (QueryTriggerInteraction)1);
		}

		private bool HandleFloorSweep()
		{
			//IL_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_028c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0293: Unknown result type (might be due to invalid IL or missing references)
			if (!FloorPickupEnabled || !Object.op_Implicit((Object)(object)ZNet.instance) || !Object.op_Implicit((Object)(object)ZNetScene.instance))
			{
				sweepingFloor = false;
				failedFloorRoutes.Clear();
				return false;
			}
			if (Time.time < nextFloorSearch)
			{
				return sweepingFloor;
			}
			bool boosted = Boosted;
			nextFloorSearch = Time.time + (boosted ? 0.6f : 2.5f);
			SortingBox[] array = (boosted ? Array.Empty<SortingBox>() : SortingBox.Loaded.Where((SortingBox box) => Time.time >= box.RetryAfter && CanUse(box.Container)).ToArray());
			HashSet<ItemDrop> drops = FindFloorDrops(array, boosted);
			KeyValuePair<(ItemDrop, CraftingStation), float>[] array2 = failedFloorRoutes.Where((KeyValuePair<(ItemDrop Drop, CraftingStation Station), float> route) => Time.time >= route.Value || !drops.Contains(route.Key.Drop) || !Object.op_Implicit((Object)(object)route.Key.Station)).ToArray();
			int amount;
			for (amount = 0; amount < array2.Length; amount++)
			{
				KeyValuePair<(ItemDrop, CraftingStation), float> keyValuePair = array2[amount];
				failedFloorRoutes.Remove(keyValuePair.Key);
			}
			foreach (ItemDrop item in drops.OrderByDescending((ItemDrop drop) => !boosted && IsTastyMead(drop.m_itemData)).ThenBy(delegate(ItemDrop drop)
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				//IL_0016: Unknown result type (might be due to invalid IL or missing references)
				//IL_001b: Unknown result type (might be due to invalid IL or missing references)
				Vector3 val = ((Component)drop).transform.position - feet;
				return ((Vector3)(ref val)).sqrMagnitude;
			}))
			{
				Vector3 position = ((Component)item).transform.position;
				Vector2i slot;
				bool hasMatch;
				if (boosted || IsTastyMead(item.m_itemData))
				{
					if (cargo == null || TryFindSlot(cargo, item.m_itemData, out slot, out amount, out hasMatch))
					{
						CraftingStation station = FindWorkbench(((Component)this).transform.position, position);
						if (TryScheduleFloorPickup(item, station, null, out var attempted))
						{
							return true;
						}
						if (attempted)
						{
							return sweepingFloor;
						}
					}
					continue;
				}
				foreach (SortingBox item2 in array.OrderBy(delegate(SortingBox box)
				{
					//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_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)
					Vector3 val = ((Component)box).transform.position - position;
					return ((Vector3)(ref val)).sqrMagnitude;
				}))
				{
					Container container = item2.Container;
					if (TryFindSlot(container.GetInventory(), item.m_itemData, out slot, out amount, out var _))
					{
						if (TryScheduleFloorPickup(item, FindWorkbench(((Component)item2).transform.position, position), container, out var attempted2))
						{
							hasMatch = true;
							return hasMatch;
						}
						if (attempted2)
						{
							hasMatch = sweepingFloor;
							return hasMatch;
						}
					}
				}
			}
			sweepingFloor = false;
			return false;
		}

		private bool TryScheduleFloorPickup(ItemDrop drop, CraftingStation station, Container box, out bool attempted)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			attempted = Object.op_Implicit((Object)(object)station) && !failedFloorRoutes.ContainsKey((drop, station));
			if (!attempted)
			{
				return false;
			}
			if (!TryRoute(((Component)drop).transform.position, 0.55f, station, ((Component)drop).transform, allowDoors: true, floorPickup: true))
			{
				MarkFloorRouteBlocked(drop, station);
				return false;
			}
			sweepingFloor = true;
			job = new Job
			{
				Source = box,
				Workbench = station,
				FloorDrop = drop,
				DirectPickup = !Object.op_Implicit((Object)(object)box)
			};
			FollowCandidateRoute();
			return true;
		}

		private void MarkFloorRouteBlocked(ItemDrop drop, CraftingStation station)
		{
			failedFloorRoutes[(drop, station)] = Time.time + 15f;
		}

		private HashSet<ItemDrop> FindFloorDrops(SortingBox[] boxes, bool boosted)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			HashSet<ItemDrop> hashSet = new HashSet<ItemDrop>();
			CraftingStation[] array = stations;
			foreach (CraftingStation station in array)
			{
				if (!Object.op_Implicit((Object)(object)station) || station.m_name != "$piece_workbench" || !WithinWorkbench(station, ((Component)this).transform.position) || !WithinWorkbench(station, feet))
				{
					continue;
				}
				Vector3 position = ((Component)station).transform.position;
				bool flag = !boosted && boxes.Any((SortingBox box) => WithinWorkbench(station, ((Component)box).transform.position));
				Vector3 val = Vector3.one * station.GetStationBuildRange();
				int num;
				while ((num = Physics.OverlapBoxNonAlloc(position, val, floorHits, Quaternion.identity, ItemLayers, (QueryTriggerInteraction)2)) == floorHits.Length)
				{
					floorHits = (Collider[])(object)new Collider[floorHits.Length * 2];
				}
				for (int num2 = 0; num2 < num; num2++)
				{
					ItemDrop componentInParent = ((Component)floorHits[num2]).GetComponentInParent<ItemDrop>();
					floorHits[num2] = null;
					if (Object.op_Implicit((Object)(object)componentInParent) && (boosted || flag || IsTastyMead(componentInParent.m_itemData)) && !hashSet.Contains(componentInParent) && WithinWorkbench(station, ((Component)componentInParent).transform.position) && CanPickUp(componentInParent, out var _))
					{
						hashSet.Add(componentInParent);
					}
				}
			}
			return hashSet;
		}

		private bool CanPickUp(ItemDrop drop, out ZNetView dropView)
		{
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			dropView = (Object.op_Implicit((Object)(object)drop) ? ((Component)drop).GetComponent<ZNetView>() : null);
			if (!Object.op_Implicit((Object)(object)ZNet.instance) || !Object.op_Implicit((Object)(object)dropView) || !dropView.IsValid() || !dropView.IsOwner() || !Object.op_Implicit((Object)(object)drop.m_itemData.m_dropPrefab) || drop.m_itemData.m_stack < 1)
			{
				return false;
			}
			if (ProductionDrops.IsOldEnough(dropView, ZNet.instance.GetTime().Ticks))
			{
				return HasWorkAccess(((Component)drop).transform.position);
			}
			return false;
		}

		private static bool WithinPickupDistance(Vector3 from, Vector3 to)
		{
			//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_0016: 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)
			Vector3 val = from - to;
			if (((Vector3)(ref val)).sqrMagnitude <= 1.69f)
			{
				return Mathf.Abs(from.y - to.y) <= 0.75f;
			}
			return false;
		}

		private void CollectFloorCargo()
		{
			//IL_004d: 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_0072: 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_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_0228: Unknown result type (might be due to invalid IL or missing references)
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Expected O, but got Unknown
			ItemDrop floorDrop = job.FloorDrop;
			Container source = job.Source;
			bool directPickup = job.DirectPickup;
			if ((!directPickup && !CanUse(source)) || !CanPickUp(floorDrop, out var dropView) || !WithinWorkbench(job.Workbench, ((Component)floorDrop).transform.position) || !WithinWorkbench(job.Workbench, DeliveryOrigin) || !WithinPickupDistance(feet, ((Component)floorDrop).transform.position) || Object.op_Implicit((Object)(object)DoorRoutes.FindObstacle(feet, ((Component)floorDrop).transform.position, ((Component)floorDrop).transform)))
			{
				FinishJob();
				return;
			}
			if (!Boosted && IsTastyMead(floorDrop.m_itemData))
			{
				TakeGroundItems(floorDrop, dropView, 1);
				DrinkTastyMead();
				job.FloorDrop = null;
				FinishJob();
				return;
			}
			Inventory val = (directPickup ? cargo : source.GetInventory());
			int num = ((val == null) ? (16 * floorDrop.m_itemData.m_shared.m_maxStackSize) : CargoRoom(val, floorDrop.m_itemData));
			int amount = Mathf.Min(floorDrop.m_itemData.m_stack, num);
			if (amount <= 0)
			{
				FinishJob();
				return;
			}
			bool flag = ProductionDrops.IsOutput(dropView);
			List<(ItemDrop, ZNetView, int)> list = new List<(ItemDrop, ZNetView, int)>();
			Vector2i slot;
			bool hasMatch;
			if (flag)
			{
				int num2 = Mathf.Min(new int[3]
				{
					num,
					10,
					floorDrop.m_itemData.m_shared.m_maxStackSize
				});
				amount = Mathf.Min(floorDrop.m_itemData.m_stack, num2);
				AddNearbyProduction(floorDrop, num2 - amount, list);
			}
			else if (!directPickup && !TryFindSlot(val, floorDrop.m_itemData, out slot, out amount, out hasMatch))
			{
				FinishJob();
				return;
			}
			list.Add((floorDrop, dropView, amount));
			ItemData val2 = floorDrop.m_itemData.Clone();
			val2.m_stack = list.Sum<(ItemDrop, ZNetView, int)>(((ItemDrop Drop, ZNetView View, int Amount) pickup) => pickup.Amount);
			int stack = val2.m_stack;
			if (directPickup)
			{
				AddDirectFloorCargo(val2);
			}
			else
			{
				val2.m_gridPos = new Vector2i(0, 0);
				cargo = new Inventory("Kvastur cargo", (Sprite)null, 1, 1);
				cargo.GetAllItems().Add(val2);
				SetCargoSource(source);
				floorCargo = true;
			}
			SaveCargo();
			foreach (var (item, itemView, amount2) in list)
			{
				TakeGroundItems(item, itemView, amount2);
			}
			job.FloorDrop = null;
			Plugin.Log.LogInfo((object)($"Kvastur picked up {stack} x {val2.m_shared.m_name} from the floor" + (directPickup ? " for direct delivery." : (" for " + ((Object)source).name + "."))));
			if (flag)
			{
				if (val2.m_shared.m_name == "$item_coal")
				{
					Speak(SpeechLine.Coal, perJob: true);
				}
				if (val2.m_shared.m_name == "$item_iron")
				{
					Speak(SpeechLine.Iron, perJob: true);
				}
			}
			if (directPickup)
			{
				FinishJob();
			}
			else
			{
				PlanReturn();
			}
		}

		private static void TakeGroundItems(ItemDrop item, ZNetView itemView, int amount)
		{
			if (amount == item.m_itemData.m_stack)
			{
				ZNetScene.instance.Destroy(((Component)item).gameObject);
				return;
			}
			ItemData itemData = item.m_itemData;
			itemData.m_stack -= amount;
			ItemDrop.SaveToZDO(item.m_itemData, itemView.GetZDO(), -1);
		}

		private void AddNearbyProduction(ItemDrop first, int remaining, List<(ItemDrop Drop, ZNetView View, int Amount)> pickups)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (migh