Decompiled source of Gunnr v1.3.1

plugins/FieldArtillery/Fred.FieldArtillery.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Threading;
using BepInEx;
using BepInEx.Logging;
using Gunworks;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Splatform;
using UnityEngine;

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

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Fred.FieldArtillery
{
	internal static class ArtMaterials
	{
		private static readonly Dictionary<string, Texture2D> Textures = new Dictionary<string, Texture2D>();

		public static void Apply(IModHost host, params Transform[] parts)
		{
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			for (int i = 0; i < parts.Length; i++)
			{
				Material[] sharedMaterials = ((Renderer)((Component)parts[i]).GetComponent<MeshRenderer>()).sharedMaterials;
				foreach (Material val in sharedMaterials)
				{
					string name = ((Object)val).name;
					int num2;
					object obj;
					if (!name.EndsWith("_pine", StringComparison.Ordinal) && !name.EndsWith("_oak", StringComparison.Ordinal))
					{
						num2 = (name.EndsWith("_ash", StringComparison.Ordinal) ? 1 : 0);
						if (num2 == 0)
						{
							obj = "surface-grit";
							goto IL_0069;
						}
					}
					else
					{
						num2 = 1;
					}
					obj = "wood-grain";
					goto IL_0069;
					IL_0069:
					string text = (string)obj;
					if (!Textures.TryGetValue(text, out var value))
					{
						value = new Texture2D(2, 2, (TextureFormat)4, true);
						if (!ImageConversion.LoadImage(value, host.ReadAsset("textures/" + text + ".png"), true))
						{
							throw new FormatException("Cannot load " + text + " texture.");
						}
						((Object)value).name = text;
						((Texture)value).wrapMode = (TextureWrapMode)0;
						((Texture)value).filterMode = (FilterMode)2;
						Textures.Add(text, value);
					}
					val.mainTexture = (Texture)(object)value;
					if (num2 != 0 || name.EndsWith("_hemp", StringComparison.Ordinal) || name.EndsWith("_flint", StringComparison.Ordinal))
					{
						if (val.HasProperty("_Metallic"))
						{
							val.SetFloat("_Metallic", 0f);
						}
						if (val.HasProperty("_Glossiness"))
						{
							val.SetFloat("_Glossiness", 0.14f);
						}
						if (val.HasProperty("_Smoothness"))
						{
							val.SetFloat("_Smoothness", 0.14f);
						}
						if (val.HasProperty("_MetallicGlossMap"))
						{
							val.SetTexture("_MetallicGlossMap", (Texture)null);
						}
						val.DisableKeyword("_METALLICGLOSSMAP");
					}
				}
			}
		}
	}
	internal static class BallistaBolts
	{
		private static readonly AmmoSpec BronzeSpec = new AmmoSpec
		{
			Prefab = "fred_bronze_bolt",
			Name = "Bronze Bolt",
			Description = "A bronze-tipped bolt for the Ballista and Polybolos.",
			Look = "BoltIron",
			Model = "bronze-ammo",
			Icon = "bronze-ammo",
			Weight = 0.1f,
			MaxStack = 100,
			Station = "forge",
			Cost = () => "Wood:10,Bronze:10"
		};

		private static AmmoSpec flintSpec;

		private static IModHost host;

		private static object bronzeAmmo;

		private static PropertyInfo ammoItem;

		private static PropertyInfo ammoSpec;

		private static MethodInfo ensureAmmo;

		private static MethodInfo registerAmmo;

		public static void Apply(Harmony harmony, IModHost modHost, Type ammoType, AmmoSpec flint, PropertyInfo itemProperty)
		{
			host = modHost;
			flintSpec = flint;
			ammoItem = itemProperty;
			ammoSpec = AccessTools.Property(ammoType, "Spec");
			ensureAmmo = AccessTools.Method(ammoType, "Ensure", (Type[])null, (Type[])null);
			registerAmmo = AccessTools.Method(ammoType, "Register", (Type[])null, (Type[])null);
			bronzeAmmo = Activator.CreateInstance(ammoType, modHost, BronzeSpec, 1f);
			harmony.Patch((MethodBase)ensureAmmo, (HarmonyMethod)null, Patch("CreateBronzeBolt"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)registerAmmo, (HarmonyMethod)null, Patch("RegisterBoltRecipes"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			static HarmonyMethod Patch(string name)
			{
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0012: Expected O, but got Unknown
				return new HarmonyMethod(typeof(BallistaBolts), name, (Type[])null);
			}
		}

		private static void CreateBronzeBolt(object __instance, GameObject __result, IEnumerable<GameObject> vanilla, Transform holder, Dictionary<string, Material> materials)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			if ((Object)(object)__result != (Object)null && ammoSpec.GetValue(__instance) == flintSpec && (Object)(GameObject)ammoItem.GetValue(bronzeAmmo) == (Object)null)
			{
				ensureAmmo.Invoke(bronzeAmmo, new object[3] { vanilla, holder, materials });
			}
		}

		private static void RegisterBoltRecipes(object __instance, ObjectDB db)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected O, but got Unknown
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			if (ammoSpec.GetValue(__instance) != flintSpec)
			{
				return;
			}
			GameObject val = (GameObject)ammoItem.GetValue(bronzeAmmo);
			GameObject val2 = (GameObject)ammoItem.GetValue(__instance);
			if ((Object)(object)val2 != (Object)null)
			{
				SetBatchSize(db, val2);
			}
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			CraftingStation val3 = db.GetItemPrefab("Hammer").GetComponent<ItemDrop>().m_itemData.m_shared.m_buildPieces.m_pieces.Select((GameObject piece) => piece.GetComponent<CraftingStation>()).FirstOrDefault((Func<CraftingStation, bool>)((CraftingStation station) => (Object)(object)station != (Object)null && ((Object)station).name == BronzeSpec.Station));
			if (!((Object)(object)val3 == (Object)null))
			{
				registerAmmo.Invoke(bronzeAmmo, new object[2] { db, val3 });
				SetBatchSize(db, val);
				ZNetScene instance = ZNetScene.instance;
				if ((Object)(object)instance != (Object)null && (Object)(object)instance.GetPrefab(BronzeSpec.Prefab) != (Object)(object)val)
				{
					host.AddPrefabs(instance, (IEnumerable<GameObject>)(object)new GameObject[1] { val });
				}
			}
		}

		private static void SetBatchSize(ObjectDB db, GameObject item)
		{
			foreach (Recipe recipe in db.m_recipes)
			{
				if ((Object)(object)recipe != (Object)null && (Object)(object)recipe.m_item != (Object)null && (Object)(object)((Component)recipe.m_item).gameObject == (Object)(object)item)
				{
					recipe.m_amount = 10;
				}
			}
		}
	}
	public sealed class CarriageControls : MonoBehaviour
	{
		private Gun gun;

		private ZNetView view;

		private Rigidbody body;

		private Vagon wagon;

		private readonly List<(Collider Player, Collider Carriage)> ignoredForTow = new List<(Collider, Collider)>();

		private readonly List<Collider> raisedSkids = new List<Collider>();

		private float originalLinear;

		private float originalAngular;

		private float parkingUntil;

		private bool parked;

		private HingeJoint[] wheelHinges;

		private JointMotor[] originalMotors;

		private bool[] originalMotorUse;

		private bool wheelsBraked;

		private static Vagon activeTow;

		private static int consumedUseFrame = -1;

		private static Action<Vagon> detach;

		private static FieldRef<Player, Vector3> detachOffset;

		private static int exitMask;

		internal static void Apply(string id)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			Harmony val = new Harmony(id);
			Type type = typeof(Gun).Assembly.GetType("Gunworks.Prefab", throwOnError: true);
			detach = AccessTools.MethodDelegate<Action<Vagon>>(AccessTools.Method(typeof(Vagon), "Detach", (Type[])null, (Type[])null), (object)null, true);
			detachOffset = AccessTools.FieldRefAccess<Player, Vector3>("m_detachOffset");
			exitMask = LayerMask.GetMask(new string[6] { "terrain", "Default", "static_solid", "Default_small", "piece", "vehicle" });
			val.Patch((MethodBase)AccessTools.Method(type, "Build", (Type[])null, (Type[])null), (HarmonyMethod)null, Method("Configure"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Vagon), "AttachTo", (Type[])null, (Type[])null), (HarmonyMethod)null, Method("TrackTow"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Vagon), "Detach", (Type[])null, (Type[])null), (HarmonyMethod)null, Method("ClearTow"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Player), "Update", (Type[])null, (Type[])null), Method("ReleaseInput"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Player), "Interact", (Type[])null, (Type[])null), Method("SuppressInteraction"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			HarmonyMethod val2 = Method("ReloadWhileMounted");
			val2.priority = 800;
			val.Patch((MethodBase)AccessTools.Method(typeof(Player), "SetControls", (Type[])null, (Type[])null), val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Gun), "OnGrant", (Type[])null, (Type[])null), (HarmonyMethod)null, Method("AimHint"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Gun), "OnUseStop", (Type[])null, (Type[])null), Method("PrepareDismount"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Handle), "Interact", (Type[])null, (Type[])null), Method("CheckTowAccess"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Handle), "GetHoverText", (Type[])null, (Type[])null), (HarmonyMethod)null, Method("TowHover"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Grips), "GetHoverText", (Type[])null, (Type[])null), (HarmonyMethod)null, Method("AimHover"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Slot), "GetHoverText", (Type[])null, (Type[])null), (HarmonyMethod)null, Method("LoadHover"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			static HarmonyMethod Method(string name)
			{
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0012: Expected O, but got Unknown
				return new HarmonyMethod(typeof(CarriageControls), name, (Type[])null);
			}
		}

		private static bool Supported(string prefab)
		{
			switch (prefab)
			{
			case "fred_flametal_pak40":
			case "fred_flint_ballista":
			case "fred_bronze_polybolos":
			case "fred_iron_cannon":
			case "fred_silver_puckle":
			case "fred_blackmetal_gatling":
			case "bf_maxim":
				return true;
			default:
				return false;
			}
		}

		private static void Configure(GameObject __result)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: 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_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: 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_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__result == (Object)null)
			{
				return;
			}
			Gun component = __result.GetComponent<Gun>();
			Vagon component2 = __result.GetComponent<Vagon>();
			if (!((Object)(object)component == (Object)null) && !((Object)(object)component2 == (Object)null) && Supported(component.m_prefab))
			{
				Transform transform = __result.transform;
				Vector3 localPosition = ((Component)component2.m_wheels[0]).transform.localPosition;
				float num = Mathf.Abs(localPosition.x) + 0.21f;
				Vector3 localPosition2 = component2.m_attachPoint.localPosition;
				localPosition2.z -= 0.52f;
				localPosition2.y = 0.68f;
				component2.m_attachPoint.localPosition = localPosition2;
				component2.m_detachDistance = Mathf.Max(component2.m_detachDistance, 3.45f);
				if ((Object)(object)component.m_seat != (Object)null)
				{
					Vector3 val = transform.InverseTransformPoint(component.m_seat.position);
					val.z = Mathf.Min(val.z, localPosition2.z + 0.86f);
					component.m_seat.position = transform.TransformPoint(val);
				}
				Handle componentInChildren = __result.GetComponentInChildren<Handle>(true);
				Grips componentInChildren2 = __result.GetComponentInChildren<Grips>(true);
				Slot componentInChildren3 = __result.GetComponentInChildren<Slot>(true);
				__result.AddComponent<CarriageControls>();
				__result.AddComponent<CarriageLoading>();
				ConfigureControl((Component)(object)componentInChildren, transform, new Vector3(0f, 0.89f, localPosition2.z + 0.15f), new Vector3(1.12f, 0.91f, 0.68f));
				ConfigureControl((Component)(object)componentInChildren2, transform, new Vector3(0f - num, Mathf.Max(1.08f, localPosition.y + 0.58f), -0.51f), new Vector3(0.81f, 0.91f, 0.78f));
				ConfigureControl((Component)(object)componentInChildren3, transform, new Vector3(num, Mathf.Max(1f, localPosition.y + 0.57f), 0.35f), new Vector3(0.85f, 0.92f, 0.84f));
			}
		}

		private static void ConfigureControl(Component control, Transform root, Vector3 center, Vector3 size)
		{
			//IL_002e: 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_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)control == (Object)null))
			{
				BoxCollider component = control.GetComponent<BoxCollider>();
				if (!((Object)(object)component == (Object)null))
				{
					control.transform.SetParent(root, false);
					control.transform.localPosition = center;
					control.transform.localRotation = Quaternion.identity;
					control.transform.localScale = Vector3.one;
					component.center = Vector3.zero;
					component.size = size;
					((Collider)component).isTrigger = true;
				}
			}
		}

		private void Awake()
		{
			gun = ((Component)this).GetComponent<Gun>();
			view = ((Component)this).GetComponent<ZNetView>();
			body = ((Component)this).GetComponent<Rigidbody>();
			wagon = ((Component)this).GetComponent<Vagon>();
			originalLinear = body.linearDamping;
			originalAngular = body.angularDamping;
			wheelHinges = (from wheel in wagon.m_wheels
				select ((Component)wheel).GetComponent<HingeJoint>() into hinge
				where (Object)(object)hinge != (Object)null
				select hinge).ToArray();
			originalMotors = wheelHinges.Select((HingeJoint hinge) => hinge.motor).ToArray();
			originalMotorUse = wheelHinges.Select((HingeJoint hinge) => hinge.useMotor).ToArray();
		}

		private void FixedUpdate()
		{
			//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_00c0: 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_00cc: 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_00db: 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_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: Unknown result type (might be due to invalid IL or missing references)
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Unknown result type (might be due to invalid IL or missing references)
			//IL_023d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_026e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c4: Unknown result type (might be due to invalid IL or missing references)
			if (parked && Time.time >= parkingUntil)
			{
				parked = false;
				ResetDamping();
			}
			Player localPlayer = Player.m_localPlayer;
			if (!view.IsValid() || !view.IsOwner())
			{
				SetWheelBrake(enabled: false);
				return;
			}
			int num;
			int num2;
			if ((Object)(object)activeTow == (Object)(object)wagon && (Object)(object)localPlayer != (Object)null)
			{
				num = (wagon.IsAttached((Character)(object)localPlayer) ? 1 : 0);
				if (num != 0)
				{
					num2 = 0;
					goto IL_00a5;
				}
			}
			else
			{
				num = 0;
			}
			num2 = ((parked || ((Object)(object)gun != (Object)null && gun.User != 0)) ? 1 : 0);
			goto IL_00a5;
			IL_00a5:
			bool wheelBrake = (byte)num2 != 0;
			SetWheelBrake(wheelBrake);
			if (num != 0)
			{
				Vector3 val = Vector3.ProjectOnPlane(body.angularVelocity, Vector3.up);
				Vector3 val2 = Vector3.Cross(((Component)this).transform.up, Vector3.up);
				body.AddTorque(val2 * 16f - val * 4.6f, (ForceMode)5);
				Vector3 val3 = ((Component)localPlayer).transform.position + wagon.m_attachOffset - wagon.m_attachPoint.position;
				val3.y = 0f;
				float magnitude = ((Vector3)(ref val3)).magnitude;
				Vector3 val4 = ((magnitude > 0.08f) ? (val3 / magnitude) : Vector3.zero);
				float num3 = Vector3.Dot(((Component)localPlayer).GetComponent<Rigidbody>().linearVelocity, val4);
				float num4 = Mathf.Max(0f, num3) * 0.32f * Mathf.InverseLerp(0.1f, 0.38f, magnitude);
				Vector3 val5 = val4 * Mathf.Clamp((magnitude - 0.08f) * 13f + num4, 0f, 11.5f);
				Vector3 linearVelocity = body.linearVelocity;
				Vector3 val6 = Vector3.MoveTowards(Vector3.ProjectOnPlane(linearVelocity, Vector3.up), val5, 78f * Time.fixedDeltaTime);
				body.linearVelocity = val6 + Vector3.up * linearVelocity.y;
				if (magnitude > 0.23f)
				{
					float num5 = Vector3.SignedAngle(Vector3.ProjectOnPlane(((Object)(object)gun == (Object)null) ? (wagon.m_attachPoint.position - ((Component)this).transform.position) : (-((Component)this).transform.forward), Vector3.up), val4, Vector3.up) * (MathF.PI / 180f);
					float num6 = Vector3.Dot(body.angularVelocity, Vector3.up);
					body.AddTorque(Vector3.up * Mathf.Clamp(num5 * 15f - num6 * 5.2f, -13f, 13f), (ForceMode)5);
				}
			}
		}

		private void OnDestroy()
		{
			RestoreTowCollisions();
		}

		private void BeginTow(Player player)
		{
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			parked = false;
			SetWheelBrake(enabled: false);
			body.linearDamping = originalLinear;
			body.angularDamping = Mathf.Max(originalAngular, 0.7f);
			RestoreTowCollisions();
			ConfigurableJoint component = ((Component)wagon).GetComponent<ConfigurableJoint>();
			if ((Object)(object)component != (Object)null)
			{
				component.xMotion = (ConfigurableJointMotion)2;
				component.yMotion = (ConfigurableJointMotion)2;
				component.zMotion = (ConfigurableJointMotion)2;
				((Joint)component).breakForce = float.PositiveInfinity;
			}
			foreach (Transform item in ((Component)this).transform)
			{
				Transform val = item;
				if (!((Object)(object)gun == (Object)null) && !(((Object)val).name != "collider") && !(val.localPosition.y > 0.31f))
				{
					BoxCollider component2 = ((Component)val).GetComponent<BoxCollider>();
					if (!((Object)(object)component2 == (Object)null) && ((Collider)component2).enabled)
					{
						((Collider)component2).enabled = false;
						raisedSkids.Add((Collider)(object)component2);
					}
				}
			}
			Collider[] componentsInChildren = ((Component)player).GetComponentsInChildren<Collider>();
			foreach (Collider val2 in componentsInChildren)
			{
				Collider[] componentsInChildren2 = ((Component)this).GetComponentsInChildren<Collider>();
				foreach (Collider val3 in componentsInChildren2)
				{
					if (!val2.isTrigger && !val3.isTrigger && val2.enabled && val3.enabled && !Physics.GetIgnoreCollision(val2, val3))
					{
						Physics.IgnoreCollision(val2, val3);
						ignoredForTow.Add((val2, val3));
					}
				}
			}
		}

		private void EndTow()
		{
			RestoreTowCollisions();
			if ((Object)(object)body == (Object)null || (Object)(object)view == (Object)null || !view.IsValid() || !view.IsOwner())
			{
				ResetDamping();
				return;
			}
			parked = true;
			parkingUntil = Time.time + 0.55f;
			body.linearDamping = Mathf.Max(originalLinear, 3f);
			body.angularDamping = Mathf.Max(originalAngular, 4f);
			SetWheelBrake(enabled: true);
		}

		private void SetWheelBrake(bool enabled)
		{
			//IL_003b: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			if (wheelsBraked == enabled)
			{
				return;
			}
			wheelsBraked = enabled;
			for (int i = 0; i < wheelHinges.Length; i++)
			{
				HingeJoint val = wheelHinges[i];
				if (!((Object)(object)val == (Object)null))
				{
					? motor;
					if (!enabled)
					{
						motor = originalMotors[i];
					}
					else
					{
						JointMotor val2 = default(JointMotor);
						((JointMotor)(ref val2)).targetVelocity = 0f;
						((JointMotor)(ref val2)).force = body.mass * 10f;
						motor = val2;
					}
					val.motor = (JointMotor)motor;
					val.useMotor = enabled || originalMotorUse[i];
				}
			}
		}

		private void ResetDamping()
		{
			if (!((Object)(object)body == (Object)null))
			{
				body.linearDamping = originalLinear;
				body.angularDamping = originalAngular;
			}
		}

		private void RestoreTowCollisions()
		{
			foreach (var item in ignoredForTow)
			{
				if ((Object)(object)item.Player != (Object)null && (Object)(object)item.Carriage != (Object)null)
				{
					Physics.IgnoreCollision(item.Player, item.Carriage, false);
				}
			}
			ignoredForTow.Clear();
			foreach (Collider raisedSkid in raisedSkids)
			{
				if ((Object)(object)raisedSkid != (Object)null)
				{
					raisedSkid.enabled = true;
				}
			}
			raisedSkids.Clear();
		}

		private static void TrackTow(Vagon __instance)
		{
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null) && !((Object)(object)((Component)__instance).GetComponent<CarriageControls>() == (Object)null) && __instance.IsAttached((Character)(object)localPlayer))
			{
				activeTow = __instance;
				CarriageControls component = ((Component)__instance).GetComponent<CarriageControls>();
				component.BeginTow(localPlayer);
				((Character)localPlayer).Message((MessageType)2, Localization.instance.Localize(((Object)(object)component.gun == (Object)null) ? FarmWagons.TowHint(__instance) : "Towing. $KEY_Use lets go. Use on the gunner controls to aim."), 0, (Sprite)null, false);
			}
		}

		private static void ClearTow(Vagon __instance)
		{
			if (!((Object)(object)activeTow != (Object)(object)__instance))
			{
				activeTow = null;
				CarriageControls component = ((Component)__instance).GetComponent<CarriageControls>();
				if ((Object)(object)component != (Object)null)
				{
					component.EndTow();
				}
			}
		}

		private static void ReleaseInput(Player __instance)
		{
			if (!((Object)(object)__instance == (Object)(object)Player.m_localPlayer))
			{
				return;
			}
			if (!((Object)(object)activeTow != (Object)null))
			{
				Gun manned = Gun.Manned;
				if (!((Object)(object)((manned != null) ? ((Component)manned).GetComponent<CarriageControls>() : null) != (Object)null))
				{
					return;
				}
			}
			if (ZInput.GetButtonDown("Use") || ZInput.GetButtonDown("JoyUse"))
			{
				HandleUsePress(__instance);
			}
		}

		internal static bool InputBlocked()
		{
			Chat instance = Chat.instance;
			if (!Hud.InRadial() && !InventoryGui.IsVisible() && !Menu.IsVisible() && !Console.IsVisible() && !TextInput.IsVisible())
			{
				if ((Object)(object)instance != (Object)null)
				{
					return instance.HasFocus();
				}
				return false;
			}
			return true;
		}

		private static void HandleUsePress(Player player)
		{
			if (InputBlocked())
			{
				return;
			}
			Gun manned = Gun.Manned;
			if ((Object)(object)((manned != null) ? ((Component)manned).GetComponent<CarriageControls>() : null) != (Object)null)
			{
				consumedUseFrame = Time.frameCount;
				player.StopDoodadControl();
			}
			else
			{
				if ((Object)(object)activeTow == (Object)null)
				{
					return;
				}
				CarriageControls component = ((Component)activeTow).GetComponent<CarriageControls>();
				if ((Object)(object)component.gun == (Object)null)
				{
					if (TryReleaseTow(player))
					{
						consumedUseFrame = Time.frameCount;
					}
				}
				else
				{
					bool aim = ZInput.GetButton("AltPlace") || ZInput.GetButton("JoyAltPlace") || ZInput.GetButton("JoyAltKeys") || LookingAtOwnAim(component, player);
					CompleteTowAction(player, aim);
				}
			}
		}

		private static bool LookingAtOwnAim(CarriageControls carriage, Player player)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: 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)
			GameCamera instance = GameCamera.instance;
			Transform grips = carriage.gun.m_grips;
			if (!((Object)(object)instance == (Object)null) && !((Object)(object)grips == (Object)null))
			{
				Vector3 val = grips.position - ((Component)player).transform.position;
				if (!(((Vector3)(ref val)).sqrMagnitude > 16f))
				{
					BoxCollider component = ((Component)grips).GetComponent<BoxCollider>();
					RaycastHit val2 = default(RaycastHit);
					if ((Object)(object)component != (Object)null)
					{
						return ((Collider)component).Raycast(new Ray(((Component)instance).transform.position, ((Component)instance).transform.forward), ref val2, 8f);
					}
					return false;
				}
			}
			return false;
		}

		internal static bool CompleteTowAction(Player player, bool aim)
		{
			CarriageControls carriageControls = (((Object)(object)activeTow == (Object)null) ? null : ((Component)activeTow).GetComponent<CarriageControls>());
			if ((Object)(object)carriageControls?.gun == (Object)null || !TryReleaseTow(player))
			{
				return false;
			}
			consumedUseFrame = Time.frameCount;
			if (aim)
			{
				carriageControls.gun.Take((Humanoid)(object)player);
			}
			return true;
		}

		private static void AimHint(Gun __instance)
		{
			if (!((Object)(object)Gun.Manned != (Object)(object)__instance) && Supported(__instance.m_prefab) && !((Object)(object)Player.m_localPlayer == (Object)null))
			{
				((Character)Player.m_localPlayer).Message((MessageType)2, Localization.instance.Localize("$KEY_SecondaryAttack to load. $KEY_Use to dismount."), 0, (Sprite)null, false);
			}
		}

		private static bool CheckTowAccess(Handle __instance, Humanoid user, bool hold)
		{
			CarriageControls carriageControls = Controls((Component)(object)__instance);
			if (hold || (Object)(object)carriageControls == (Object)null || (Object)(object)carriageControls.gun == (Object)null || carriageControls.gun.User == 0L || (Object)(object)Player.GetPlayer(carriageControls.gun.User) == (Object)null)
			{
				return true;
			}
			((Character)user).Message((MessageType)2, "The gunner must dismount before towing.", 0, (Sprite)null, false);
			return false;
		}

		private static void PrepareDismount(Gun __instance, Player player)
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			CarriageControls component = ((Component)__instance).GetComponent<CarriageControls>();
			Transform seat = __instance.m_seat;
			if (!((Object)(object)component == (Object)null) && !((Object)(object)player != (Object)(object)Player.m_localPlayer) && !((Object)(object)seat == (Object)null) && !((Object)(object)player.GetAttachPoint() != (Object)(object)seat) && component.TryFindExit(player, seat, out var exit))
			{
				detachOffset.Invoke(player) = seat.InverseTransformPoint(exit);
			}
		}

		private bool TryFindExit(Player player, Transform seat, out Vector3 exit)
		{
			//IL_0026: 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_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_0087: 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_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: 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_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_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: 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_0217: Unknown result type (might be due to invalid IL or missing references)
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0229: Unknown result type (might be due to invalid IL or missing references)
			//IL_022e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Unknown result type (might be due to invalid IL or missing references)
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Unknown result type (might be due to invalid IL or missing references)
			//IL_023e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Unknown result type (might be due to invalid IL or missing references)
			float num = Mathf.Abs(((Component)wagon.m_wheels[0]).transform.localPosition.x) + 0.72f;
			Vector3 val = Vector3.ProjectOnPlane(((Component)this).transform.right, Vector3.up);
			Vector3 normalized = ((Vector3)(ref val)).normalized;
			val = Vector3.ProjectOnPlane(((Component)this).transform.forward, Vector3.up);
			Vector3 val2 = -((Vector3)(ref val)).normalized;
			Vector3[] array = (Vector3[])(object)new Vector3[3]
			{
				-normalized * num + val2 * 0.24f,
				normalized * num + val2 * 0.24f,
				val2 * 1.15f
			};
			foreach (Vector3 val3 in array)
			{
				RaycastHit val4 = (from hit in Physics.RaycastAll(seat.position + val3 + Vector3.up, Vector3.down, 2.1f, exitMask, (QueryTriggerInteraction)1)
					where !((Component)((RaycastHit)(ref hit)).collider).transform.IsChildOf(((Component)this).transform) && !((Component)((RaycastHit)(ref hit)).collider).transform.IsChildOf(((Component)player).transform)
					orderby ((RaycastHit)(ref hit)).distance
					select hit).FirstOrDefault();
				if ((Object)(object)((RaycastHit)(ref val4)).collider == (Object)null || ((RaycastHit)(ref val4)).normal.y < 0.65f)
				{
					continue;
				}
				Vector3 val5 = ((RaycastHit)(ref val4)).point + Vector3.up * 0.055f;
				Vector3 val6 = val5 + Vector3.up * 0.44f;
				if (!Physics.OverlapCapsule(val6, val5 + Vector3.up * 1.42f, 0.32f, exitMask, (QueryTriggerInteraction)1).Any((Collider collider) => !((Component)collider).transform.IsChildOf(((Component)player).transform)))
				{
					Vector3 val7 = val6 - (seat.position + Vector3.up * 0.44f);
					if (!Physics.RaycastAll(val6 - val7, ((Vector3)(ref val7)).normalized, ((Vector3)(ref val7)).magnitude, exitMask, (QueryTriggerInteraction)1).Any((RaycastHit hit) => !((Component)((RaycastHit)(ref hit)).collider).transform.IsChildOf(((Component)this).transform) && !((Component)((RaycastHit)(ref hit)).collider).transform.IsChildOf(((Component)player).transform)))
					{
						exit = val5;
						return true;
					}
				}
			}
			exit = Vector3.zero;
			return false;
		}

		private static void ReloadWhileMounted(Player __instance, ref bool secondaryAttack, ref bool secondaryAttackHold)
		{
			Gun manned = Gun.Manned;
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && !((Object)(object)manned == (Object)null) && Supported(manned.m_prefab))
			{
				bool num = secondaryAttack;
				secondaryAttack = false;
				secondaryAttackHold = false;
				if (num && !InputBlocked())
				{
					manned.LoadFrom((Humanoid)(object)__instance);
				}
			}
		}

		internal static bool TryReleaseTow(Player player)
		{
			if ((Object)(object)activeTow == (Object)null || !activeTow.IsAttached((Character)(object)player))
			{
				return false;
			}
			detach(activeTow);
			activeTow = null;
			return true;
		}

		internal static Vagon CurrentTow(Player player)
		{
			if (!((Object)(object)activeTow != (Object)null) || !activeTow.IsAttached((Character)(object)player))
			{
				return null;
			}
			return activeTow;
		}

		private static bool SuppressInteraction(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				return Time.frameCount != consumedUseFrame;
			}
			return true;
		}

		private static CarriageControls Controls(Component control)
		{
			return control.GetComponentInParent<CarriageControls>();
		}

		private static void TowHover(Handle __instance, ref string __result)
		{
			if (!((Object)(object)Controls((Component)(object)__instance) == (Object)null))
			{
				__result = Localization.instance.Localize("Tow handle\n[<color=yellow><b>$KEY_Use</b></color>] Pull or let go");
			}
		}

		private static void AimHover(Grips __instance, ref string __result)
		{
			if (!((Object)(object)Controls((Component)(object)__instance) == (Object)null))
			{
				__result = Localization.instance.Localize("Gunner controls\n[<color=yellow><b>$KEY_Use</b></color>] Aim or dismount");
			}
		}

		private static void LoadHover(Slot __instance, ref string __result)
		{
			CarriageControls carriageControls = Controls((Component)(object)__instance);
			if (!((Object)(object)carriageControls == (Object)null))
			{
				Gun obj = carriageControls.gun;
				if (((obj == null) ? null : obj.Spec?.Feed) != null)
				{
					FeedSpec feed = carriageControls.gun.Spec.Feed;
					string prefab = carriageControls.gun.m_prefab;
					string text = ((prefab == "fred_flint_ballista" || prefab == "fred_bronze_polybolos") ? "Flint or Bronze Bolt" : feed.Item.Name);
					string arg = (carriageControls.gun.Busy ? "Reloading" : ((carriageControls.gun.Rounds + feed.RoundsPerItem > feed.Most) ? "Full" : "Ready"));
					__result = Localization.instance.Localize($"Loading point · {carriageControls.gun.Rounds}/{feed.Most} · {arg}\n" + "[<color=yellow><b>$KEY_Use</b></color>] Load " + text);
				}
			}
		}
	}
	internal sealed class CarriageLoading : MonoBehaviour
	{
		private const string RequestRpc = "fred_fieldartillery_load_request";

		private const string ReplyRpc = "fred_fieldartillery_load_reply";

		private static long nextRequest = DateTime.UtcNow.Ticks;

		private Gun gun;

		private Feed feed;

		private ZNetView view;

		private Player loader;

		private ItemData pending;

		private long pendingId;

		private double retryAt;

		internal static void Apply(string id)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Expected O, but got Unknown
			Harmony harmony = new Harmony(id);
			Patch(typeof(Feed), "Load", "Load");
			Patch(typeof(Slot), "Interact", "Interact");
			Patch(typeof(Slot), "UseItem", "UseItem");
			void Patch(Type owner, string method, string prefix)
			{
				//IL_001b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0029: Expected O, but got Unknown
				harmony.Patch((MethodBase)AccessTools.Method(owner, method, (Type[])null, (Type[])null), new HarmonyMethod(typeof(CarriageLoading), prefix, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		private void Awake()
		{
			gun = ((Component)this).GetComponent<Gun>();
			feed = ((Component)this).GetComponent<Feed>();
			view = ((Component)this).GetComponent<ZNetView>();
			if (view.IsValid())
			{
				view.Register<long>("fred_fieldartillery_load_request", (Action<long, long>)OnLoadRequest);
				view.Register<long, bool>("fred_fieldartillery_load_reply", (Action<long, long, bool>)OnLoadReply);
			}
		}

		private void Update()
		{
			if (pending != null && view.IsValid() && Time.unscaledTimeAsDouble >= retryAt)
			{
				Send();
			}
		}

		private static bool Load(Humanoid user, Gun gun, ref bool __result)
		{
			CarriageLoading component = ((Component)gun).GetComponent<CarriageLoading>();
			if ((Object)(object)component == (Object)null)
			{
				return true;
			}
			__result = component.Request(user, null);
			return false;
		}

		private static bool Interact(Slot __instance, Humanoid user, bool hold, ref bool __result)
		{
			CarriageLoading componentInParent = ((Component)__instance).GetComponentInParent<CarriageLoading>();
			if ((Object)(object)componentInParent == (Object)null)
			{
				return true;
			}
			__result = !hold && componentInParent.Request(user, null);
			return false;
		}

		private static bool UseItem(Slot __instance, Humanoid user, ItemData item, ref bool __result)
		{
			CarriageLoading componentInParent = ((Component)__instance).GetComponentInParent<CarriageLoading>();
			if ((Object)(object)componentInParent == (Object)null)
			{
				return true;
			}
			__result = item != null && componentInParent.Request(user, item);
			return false;
		}

		private bool Request(Humanoid user, ItemData selected)
		{
			Player val = (Player)(object)((user is Player) ? user : null);
			if (val == null || (Object)(object)val != (Object)(object)Player.m_localPlayer || !view.IsValid() || feed.Spec == null)
			{
				return false;
			}
			string prefab = gun.m_prefab;
			bool flag = prefab == "fred_flint_ballista" || prefab == "fred_bronze_polybolos";
			string name = feed.Spec.Item.Name;
			Inventory inventory = ((Humanoid)val).GetInventory();
			if (selected != null && selected.m_shared.m_name != name && (!flag || !(selected.m_shared.m_name == "Bronze Bolt")))
			{
				return false;
			}
			if (selected == null)
			{
				selected = inventory.GetItem(name, -1, false) ?? (flag ? inventory.GetItem("Bronze Bolt", -1, false) : null);
			}
			if (pending != null)
			{
				return Message(val, "Waiting for the carriage to confirm the load.");
			}
			if (gun.Busy)
			{
				return Message(val, "Reloading.");
			}
			if (!HasRoom())
			{
				return Message(val, "Ammunition is full.");
			}
			if (selected == null || !inventory.ContainsItem(selected))
			{
				return Message(val, flag ? "You need Flint Bolts or Bronze Bolts." : ("You need " + name + "."));
			}
			ItemData val2 = selected.Clone();
			val2.m_stack = 1;
			val2.m_equipped = false;
			if (!inventory.RemoveItem(selected, 1))
			{
				return false;
			}
			loader = val;
			pending = val2;
			pendingId = Interlocked.Increment(ref nextRequest);
			Send();
			return true;
		}

		private void Send()
		{
			retryAt = Time.unscaledTimeAsDouble + 1.0;
			view.InvokeRPC("fred_fieldartillery_load_request", new object[1] { pendingId });
		}

		private bool HasRoom()
		{
			return feed.Loaded + feed.Spec.RoundsPerItem <= feed.Spec.Most;
		}

		private void OnLoadRequest(long sender, long request)
		{
			if (!view.IsOwner() || request <= 0 || feed.Spec == null)
			{
				return;
			}
			ZDO zDO = view.GetZDO();
			string text = $"fred_fieldartillery_load_{sender}";
			long num = zDO.GetLong(text, 0L);
			if (Math.Abs(num) >= request)
			{
				view.InvokeRPC(sender, "fred_fieldartillery_load_reply", new object[2]
				{
					request,
					num == request
				});
				return;
			}
			bool flag = !gun.Busy && HasRoom();
			zDO.Set(text, flag ? request : (-request));
			if (flag)
			{
				zDO.Set(gun.Spec.Keys + "_rounds", feed.Loaded + feed.Spec.RoundsPerItem);
				view.InvokeRPC(ZNetView.Everybody, gun.LoadShowRpc, Array.Empty<object>());
			}
			view.InvokeRPC(sender, "fred_fieldartillery_load_reply", new object[2] { request, flag });
		}

		private void OnLoadReply(long sender, long request, bool accepted)
		{
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			if (request != pendingId || pending == null || sender != view.GetZDO().GetOwner())
			{
				return;
			}
			ItemData val = pending;
			Player val2 = loader;
			pending = null;
			loader = null;
			if ((Object)(object)val2 == (Object)null)
			{
				return;
			}
			if (accepted)
			{
				Message(val2, "Loaded " + val.m_shared.m_name + ".");
				return;
			}
			if (!((Humanoid)val2).GetInventory().AddItem(val))
			{
				ItemDrop.DropItem(val, 1, ((Component)val2).transform.position + Vector3.up, Quaternion.identity);
			}
			Message(val2, "The carriage is already loaded or reloading. Ammunition returned.");
		}

		private static bool Message(Player player, string text)
		{
			((Character)player).Message((MessageType)2, text, 0, (Sprite)null, false);
			return false;
		}
	}
	internal static class FarmCrops
	{
		private const string PickRpc = "FA_CombineCrop";

		private const string CargoKey = "fred.fieldartillery.farm.cargo";

		private const float CollectRange = 24f;

		private static readonly Dictionary<string, Plant> PlantBySeed = new Dictionary<string, Plant>();

		private static readonly HashSet<int> FieldCrops = new HashSet<int>();

		private static readonly Collider[] HitColliders = (Collider[])(object)new Collider[128];

		private static readonly HashSet<Pickable> Seen = new HashSet<Pickable>();

		private static readonly Dictionary<ZDOID, float> Pending = new Dictionary<ZDOID, float>();

		private static int plantingMask;

		private static int roofMask;

		private static float nextSweep;

		private static List<ItemDrop> drops;

		internal static void Apply(Harmony harmony)
		{
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Expected O, but got Unknown
			plantingMask = LayerMask.GetMask(new string[5] { "Default", "static_solid", "Default_small", "piece", "piece_nonsolid" });
			roofMask = LayerMask.GetMask(new string[3] { "Default", "static_solid", "piece" });
			drops = (List<ItemDrop>)AccessTools.Field(typeof(ItemDrop), "s_instances").GetValue(null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Pickable), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(FarmCrops), "RegisterHarvest", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		internal static void ReadCultivator(ObjectDB db)
		{
			PlantBySeed.Clear();
			FieldCrops.Clear();
			GameObject itemPrefab = db.GetItemPrefab("Cultivator");
			PieceTable val = ((itemPrefab != null) ? itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared.m_buildPieces : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			foreach (GameObject piece in val.m_pieces)
			{
				Piece component = piece.GetComponent<Piece>();
				Plant component2 = piece.GetComponent<Plant>();
				if ((Object)(object)component2 == (Object)null || (Object)(object)component == (Object)null || !component2.m_needCultivatedGround || component2.m_attachDistance > 0f || component2.m_growRadius >= 1.6199999f || component.m_resources.Length != 1 || component.m_resources[0].m_amount != 1 || (Object)(object)component.m_resources[0].m_resItem == (Object)null || !component2.m_grownPrefabs.Any((GameObject grown) => (Object)(object)grown != (Object)null && (Object)(object)grown.GetComponent<Pickable>() != (Object)null))
				{
					continue;
				}
				PlantBySeed[((Object)((Component)component.m_resources[0].m_resItem).gameObject).name] = component2;
				GameObject[] grownPrefabs = component2.m_grownPrefabs;
				foreach (GameObject val2 in grownPrefabs)
				{
					if ((Object)(object)val2 != (Object)null && (Object)(object)val2.GetComponent<Pickable>() != (Object)null)
					{
						FieldCrops.Add(StringExtensionMethods.GetStableHashCode(((Object)val2).name));
					}
				}
			}
		}

		internal static ItemData FirstSeed(Inventory hopper, out Plant plant)
		{
			plant = null;
			int num = int.MaxValue;
			ItemData result = null;
			foreach (ItemData allItem in hopper.GetAllItems())
			{
				int num2 = allItem.m_gridPos.y * hopper.GetWidth() + allItem.m_gridPos.x;
				if (num2 < num && allItem.m_stack >= 1 && !((Object)(object)allItem.m_dropPrefab == (Object)null) && PlantBySeed.TryGetValue(((Object)allItem.m_dropPrefab).name, out var value))
				{
					num = num2;
					result = allItem;
					plant = value;
				}
			}
			return result;
		}

		internal static bool Sow(Vector3 target, Player driver, ItemData seed, Plant plant)
		{
			//IL_0000: 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_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: 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_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Invalid comparison between Unknown and I4
			//IL_0065: 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_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Invalid comparison between Unknown and I4
			//IL_004a: 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_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Invalid comparison between Unknown and I4
			//IL_00b3: 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_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			if (!FarmSoil.TryGround(target, out var ground, out var map) || !map.IsCultivated(ground) || !FarmWagons.Access(ground, plant.m_growRadius))
			{
				return false;
			}
			Biome biome = map.GetBiome(ground, 0.02f, false);
			if ((biome & plant.m_biome) == 0 || (!plant.m_tolerateHeat && (int)biome == 32 && !ShieldGenerator.IsInsideShield(ground)) || (!plant.m_tolerateCold && ((int)biome == 4 || (int)biome == 64)))
			{
				return false;
			}
			if (Physics.Raycast(ground + Vector3.up * 0.02f, Vector3.up, 100f, roofMask, (QueryTriggerInteraction)1) || Physics.CheckSphere(ground, plant.m_growRadius + 0.1f, plantingMask, (QueryTriggerInteraction)2))
			{
				return false;
			}
			Piece val = default(Piece);
			if (Object.Instantiate<GameObject>(((Component)plant).gameObject, ground, Quaternion.Euler(0f, Random.Range(0f, 360f), 0f)).TryGetComponent<Piece>(ref val))
			{
				val.SetCreator(driver.GetPlayerID(), ((IUser)PlatformManager.DistributionPlatform.LocalUser).PlatformUserID);
				val.m_placeEffect.Create(ground, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
			}
			return true;
		}

		internal static void Harvest(ZDOID wagon, Vector3 center, Vector3 heading, Inventory cargo)
		{
			//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_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: 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_01d5: Unknown result type (might be due to invalid IL or missing references)
			int num = Physics.OverlapBoxNonAlloc(center + Vector3.up * 0.12f, new Vector3(5.3999996f, 2.15f, 0.95f), HitColliders, Quaternion.LookRotation(heading), plantingMask, (QueryTriggerInteraction)2);
			Seen.Clear();
			if (Time.time >= nextSweep)
			{
				nextSweep = Time.time + 3f;
				ZDOID[] array = (from entry in Pending
					where entry.Value <= Time.time
					select entry.Key).ToArray();
				foreach (ZDOID key in array)
				{
					Pending.Remove(key);
				}
			}
			for (int num3 = 0; num3 < num; num3++)
			{
				Pickable componentInParent = ((Component)HitColliders[num3]).GetComponentInParent<Pickable>();
				if ((Object)(object)componentInParent == (Object)null || !Seen.Add(componentInParent) || !FarmWagons.Access(((Component)componentInParent).transform.position))
				{
					continue;
				}
				ZNetView component = ((Component)componentInParent).GetComponent<ZNetView>();
				if (!CanHarvest(componentInParent, component))
				{
					continue;
				}
				ItemData val = componentInParent.m_itemPrefab.GetComponent<ItemDrop>().m_itemData.Clone();
				val.m_worldLevel = (byte)Game.m_worldLevel;
				if (cargo.CanAddItem(val, PrimaryYield(componentInParent)))
				{
					ZDOID uid = component.GetZDO().m_uid;
					if (!Pending.TryGetValue(uid, out var value) || !(value > Time.time))
					{
						Pending[uid] = Time.time + 1.4f;
						component.InvokeRPC("FA_CombineCrop", new object[1] { wagon });
						Collect(wagon, center, cargo);
					}
				}
			}
		}

		private static bool CanHarvest(Pickable crop, ZNetView view)
		{
			if ((Object)(object)view != (Object)null && view.IsValid() && FieldCrops.Contains(view.GetZDO().GetPrefab()) && !crop.GetPicked() && crop.CanBePicked() && crop.GetEnabled == 1 && (Object)(object)crop.m_itemPrefab != (Object)null)
			{
				return (Object)(object)crop.m_itemPrefab.GetComponent<ItemDrop>() != (Object)null;
			}
			return false;
		}

		private static int PrimaryYield(Pickable crop)
		{
			if (!crop.m_dontScale)
			{
				return Mathf.Max(crop.m_minAmountScaled, Game.instance.ScaleDrops(crop.m_itemPrefab, crop.m_amount));
			}
			return crop.m_amount;
		}

		private static void RegisterHarvest(Pickable __instance, ZNetView ___m_nview)
		{
			if ((Object)(object)___m_nview == (Object)null || !___m_nview.IsValid())
			{
				return;
			}
			___m_nview.Register<ZDOID>("FA_CombineCrop", (Action<long, ZDOID>)delegate(long sender, ZDOID wagon)
			{
				//IL_0021: 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_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_0063: 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_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_0085: 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_0087: Unknown result type (might be due to invalid IL or missing references)
				//IL_0082: Unknown result type (might be due to invalid IL or missing references)
				//IL_008c: Unknown result type (might be due to invalid IL or missing references)
				//IL_009a: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
				if (___m_nview.IsOwner() && CanHarvest(__instance, ___m_nview) && FarmWagons.VerifyWorkingWagon(sender, wagon, FarmImplement.Combine, ((Component)__instance).transform.position))
				{
					Pickable val = __instance;
					Vector3 position = ((Component)val).transform.position;
					Vector3 val2 = (val.m_spawnOffsetLocalTransform ? ((Component)val).transform.up : Vector3.up) * val.m_spawnOffset;
					val.m_pickEffector.Create(val.m_pickEffectAtSpawnPoint ? (position + val2) : position, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
					SpawnTagged(val.m_itemPrefab, PrimaryYield(val), position + val2, wagon);
					if (!val.m_extraDrops.IsEmpty())
					{
						foreach (ItemData dropListItem in val.m_extraDrops.GetDropListItems())
						{
							SpawnTagged(dropListItem.m_dropPrefab, dropListItem.m_stack, position + val2, wagon);
						}
					}
					___m_nview.InvokeRPC(ZNetView.Everybody, "RPC_SetPicked", new object[1] { true });
				}
			});
		}

		private static void SpawnTagged(GameObject item, int amount, Vector3 position, ZDOID wagon)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			ItemDrop val = (((Object)(object)item == (Object)null) ? null : item.GetComponent<ItemDrop>());
			if (!((Object)(object)val == (Object)null))
			{
				int num = Mathf.Max(1, val.m_itemData.m_shared.m_maxStackSize);
				while (amount > 0)
				{
					int num2 = Mathf.Min(amount, num);
					ItemDrop component = Object.Instantiate<GameObject>(item, position, Quaternion.identity).GetComponent<ItemDrop>();
					component.SetStack(num2);
					ItemDrop.OnCreateNew(component, false);
					((Component)component).GetComponent<ZNetView>().GetZDO().Set("fred.fieldartillery.farm.cargo", wagon);
					amount -= num2;
				}
			}
		}

		internal static void Collect(ZDOID wagon, Vector3 position, Inventory cargo)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_00ca: Unknown result type (might be due to invalid IL or missing references)
			for (int num = drops.Count - 1; num >= 0; num--)
			{
				ItemDrop val = drops[num];
				if (!((Object)(object)val == (Object)null))
				{
					Vector3 val2 = ((Component)val).transform.position - position;
					if (!(((Vector3)(ref val2)).sqrMagnitude > 576f))
					{
						ZNetView component = ((Component)val).GetComponent<ZNetView>();
						if (!((Object)(object)component == (Object)null) && component.IsValid() && !(component.GetZDO().GetZDOID("fred.fieldartillery.farm.cargo") != wagon))
						{
							if (!component.IsOwner())
							{
								val.RequestOwn();
							}
							else
							{
								val.Load();
								ItemData itemData = val.m_itemData;
								int num2 = SurtlingHarvest.Store(cargo, itemData, itemData.m_stack);
								if (num2 == itemData.m_stack)
								{
									component.Destroy();
								}
								else
								{
									component.GetZDO().Set("fred.fieldartillery.farm.cargo", ZDOID.None);
									if (num2 > 0)
									{
										val.SetStack(itemData.m_stack - num2);
									}
								}
							}
						}
					}
				}
			}
		}
	}
	internal static class FarmSoil
	{
		private const string CultivateRpc = "FA_CultivateRows";

		private const float FurrowRadius = 1.08f;

		private static readonly Settings Furrow = new Settings
		{
			m_paintType = (PaintType)1,
			m_paintCleared = true,
			m_paintRadius = 1.08f,
			m_paintHeightCheck = false,
			m_halfOffset = false
		};

		private static readonly List<Heightmap> AffectedMaps = new List<Heightmap>();

		private static readonly Dictionary<TerrainComp, List<Vector3>> Batches = new Dictionary<TerrainComp, List<Vector3>>();

		private static readonly Stack<List<Vector3>> BatchPool = new Stack<List<Vector3>>();

		private static Action<TerrainComp, Vector3, Vector3, Settings> paint;

		private static Action<TerrainComp, bool> save;

		private static int terrainMask;

		private static EffectList effects;

		internal static void Apply(Harmony harmony)
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			terrainMask = LayerMask.GetMask(new string[1] { "terrain" });
			paint = AccessTools.MethodDelegate<Action<TerrainComp, Vector3, Vector3, Settings>>(AccessTools.Method(typeof(TerrainComp), "InternalDoOperation", (Type[])null, (Type[])null), (object)null, true);
			save = AccessTools.MethodDelegate<Action<TerrainComp, bool>>(AccessTools.Method(typeof(TerrainComp), "Save", (Type[])null, (Type[])null), (object)null, true);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(TerrainComp), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(FarmSoil), "RegisterCompiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		internal static void ReadCultivator(ObjectDB db)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Invalid comparison between Unknown and I4
			foreach (TerrainOp terrainOp in db.m_terrainOps)
			{
				if ((int)terrainOp.m_settings.m_paintType == 1 && !terrainOp.m_settings.m_level)
				{
					effects = terrainOp.m_onPlacedEffect;
					break;
				}
			}
		}

		internal static bool TryGround(Vector3 target, out Vector3 ground, out Heightmap map)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: 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_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_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			ground = default(Vector3);
			map = null;
			RaycastHit val = default(RaycastHit);
			if (!Physics.Raycast(target + Vector3.up * 2.4f, Vector3.down, ref val, 4.6f, terrainMask, (QueryTriggerInteraction)1) || ((RaycastHit)(ref val)).normal.y < 0.68f || ((RaycastHit)(ref val)).point.y < ZoneSystem.instance.m_waterLevel + 0.15f)
			{
				return false;
			}
			ground = ((RaycastHit)(ref val)).point;
			map = ((Component)((RaycastHit)(ref val)).collider).GetComponent<Heightmap>() ?? Heightmap.FindHeightmap(ground);
			if ((Object)(object)map != (Object)null)
			{
				return !map.IsLava(ground, 0.6f);
			}
			return false;
		}

		internal static void Cultivate(ZDOID wagon, List<Vector3> points)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Expected O, but got Unknown
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			if (points.Count == 0)
			{
				return;
			}
			foreach (Vector3 point in points)
			{
				AffectedMaps.Clear();
				Heightmap.FindHeightmap(point, 1.08f, AffectedMaps);
				foreach (Heightmap affectedMap in AffectedMaps)
				{
					TerrainComp andCreateTerrainCompiler = affectedMap.GetAndCreateTerrainCompiler();
					if (!((Object)(object)andCreateTerrainCompiler == (Object)null))
					{
						if (!Batches.TryGetValue(andCreateTerrainCompiler, out var value))
						{
							Batches.Add(andCreateTerrainCompiler, value = ((BatchPool.Count > 0) ? BatchPool.Pop() : new List<Vector3>(6)));
						}
						value.Add(point);
					}
				}
			}
			foreach (KeyValuePair<TerrainComp, List<Vector3>> batch in Batches)
			{
				ZPackage val = new ZPackage();
				val.Write(wagon);
				val.Write(batch.Value.Count);
				foreach (Vector3 item in batch.Value)
				{
					val.Write(item);
				}
				((Component)batch.Key).GetComponent<ZNetView>().InvokeRPC("FA_CultivateRows", new object[1] { val });
				batch.Value.Clear();
				BatchPool.Push(batch.Value);
			}
			Batches.Clear();
			EffectList obj = effects;
			if (obj != null)
			{
				obj.Create(points[points.Count / 2], Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
			}
		}

		private static void RegisterCompiler(TerrainComp __instance, ZNetView ___m_nview, Heightmap ___m_hmap)
		{
			if (!((Object)(object)___m_nview == (Object)null) && ___m_nview.IsValid() && !((Object)(object)___m_hmap == (Object)null))
			{
				___m_nview.Register<ZPackage>("FA_CultivateRows", (Action<long, ZPackage>)delegate(long sender, ZPackage pkg)
				{
					ApplyRows(__instance, ___m_hmap, sender, pkg);
				});
			}
		}

		private static void ApplyRows(TerrainComp compiler, Heightmap map, long sender, ZPackage packet)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: 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_0050: 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_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_009b: 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)
			if (!compiler.IsOwner())
			{
				return;
			}
			ZDOID id = packet.ReadZDOID();
			int num = packet.ReadInt();
			if (num < 1 || num > 6)
			{
				return;
			}
			Vector3 val = Vector3.zero;
			int num2 = 0;
			for (int i = 0; i < num; i++)
			{
				Vector3 val2 = packet.ReadVector3();
				if (FarmWagons.VerifyWorkingWagon(sender, id, FarmImplement.Plow, val2) && !Location.IsInsideNoBuildLocation(val2))
				{
					paint(compiler, val2, Vector3.zero, Furrow);
					val += val2;
					num2++;
				}
			}
			if (num2 != 0)
			{
				save(compiler, arg2: true);
				map.Poke(1, true);
				ClutterSystem instance = ClutterSystem.instance;
				if (instance != null)
				{
					instance.ResetGrass(val / (float)num2, 10.799999f);
				}
			}
		}
	}
	internal static class FarmWagonArt
	{
		private const float BeamDepth = 0.66f;

		private static float HalfSpan => 4.5f;

		internal static void Build(ObjectDB db, Vagon wagon, FarmImplement kind, out Transform fixture, out Transform reel)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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_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_003b: 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_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: 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_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0208: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: 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_0241: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_0281: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_030a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0310: Unknown result type (might be due to invalid IL or missing references)
			//IL_0361: Unknown result type (might be due to invalid IL or missing references)
			//IL_0367: Unknown result type (might be due to invalid IL or missing references)
			//IL_038c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0392: Unknown result type (might be due to invalid IL or missing references)
			Transform val = WagonModel.Child(((Component)wagon).transform, "Farming equipment", Vector3.zero);
			Vector3 val2 = Vector3.ProjectOnPlane(wagon.m_attachPoint.localPosition, Vector3.up);
			val.localRotation = ((((Vector3)(ref val2)).sqrMagnitude > 0.01f) ? Quaternion.LookRotation(-val2) : Quaternion.identity);
			fixture = WagonModel.Child(val, "Lowering frame", new Vector3(0f, 0.69f, 0.89f));
			reel = null;
			WagonModel wagonModel = new WagonModel(db, val);
			wagonModel.Box("rawwood", 0f, 0.88f, 0.36f, 1.71f, 0.16f, 0.21f);
			wagonModel.Box("wood", 0f, 0.81f, 0.81f, 1.91f, 0.17f, 0.15f);
			for (int i = -1; i <= 1; i += 2)
			{
				wagonModel.Beam("wood", new Vector3((float)i * 0.5f, 0.83f, -0.29f), new Vector3((float)i * 0.88f, 0.83f, 0.91f), 0.15f, 0.15f);
				wagonModel.Box("bronze", (float)i * 0.73f, 0.9f, 0.68f, 0.15f, 0.035f, 0.33f);
				wagonModel.Bearing((float)i * 0.79f, 0.72f, 0.88f, 0.116f, 0.12f, "iron");
				wagonModel.Bearing((float)i * 0.854f, 0.72f, 0.88f, 0.048f, 0.022f);
			}
			wagonModel.Commit();
			WagonModel wagonModel2 = new WagonModel(db, fixture);
			wagonModel2.Box("rawwood", 0f, -0.03f, 0.66f, 2f * HalfSpan + 0.47f, 0.154f, 0.19f);
			wagonModel2.Box("wood", 0f, 0.14f, 0.28f, 2.07f, 0.17f, 0.15f);
			for (int j = -1; j <= 1; j += 2)
			{
				wagonModel2.Beam("wood", new Vector3((float)j * 0.61f, 0.16f, 0.25f), new Vector3((float)j * HalfSpan, -0.028f, 0.66f), 0.12f, 0.143f);
				wagonModel2.Beam("leather", new Vector3((float)j * 0.79f, 0.32f, 0.25f), new Vector3((float)j * HalfSpan, 0.032f, 0.66f), 0.038f, 0.034f);
				wagonModel2.Box("bronze", (float)j * (HalfSpan + 0.18f), -0.03f, 0.66f, 0.14f, 0.165f, 0.21f);
			}
			for (int k = 0; k < 6; k++)
			{
				float x = (float)k * 1.8f - HalfSpan;
				wagonModel2.Box("iron", x, -0.03f, 0.66f, 0.21f, 0.172f, 0.23f);
				wagonModel2.Cylinder("bronze", x, 0.062f, 0.66f, 0.036f, 0.025f);
				switch (kind)
				{
				case FarmImplement.Plow:
					AddPlowshare(wagonModel2, x);
					break;
				case FarmImplement.Sower:
					AddSeedOutlet(wagonModel2, x);
					break;
				case FarmImplement.Combine:
					AddCutter(wagonModel2, x);
					break;
				}
			}
			wagonModel2.Commit();
			if (kind == FarmImplement.Sower)
			{
				AddHopper(db, val);
			}
			if (kind == FarmImplement.Combine)
			{
				reel = AddReel(db, fixture);
			}
		}

		private static void AddPlowshare(WagonModel tool, float x)
		{
			//IL_0011: 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_0064: 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_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			tool.Beam("rawwood", new Vector3(x, -0.065f, 0.61f), new Vector3(x, -0.49f, 0.93f), 0.135f, 0.15f);
			tool.Box("iron", x, -0.48f, 0.9f, 0.16f, 0.25f, 0.21f, new Vector3(-23f, 0f, 0f));
			tool.Box("bronze", x - 0.067f, -0.558f, 0.98f, 0.26f, 0.08f, 0.43f, new Vector3(-15f, 0f, 14f));
			tool.Box("bronze", x + 0.118f, -0.479f, 1.015f, 0.078f, 0.215f, 0.36f, new Vector3(-22f, 19f, 11f));
			tool.Box("iron", x - 0.036f, -0.579f, 1.153f, 0.11f, 0.066f, 0.18f, new Vector3(-26f, 16f, 0f));
		}

		private static void AddSeedOutlet(WagonModel tool, float x)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: 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)
			float num = x * 0.13f;
			Vector3 start = default(Vector3);
			((Vector3)(ref start))..ctor(num, 0.29f, 0.05f);
			Vector3 val = default(Vector3);
			((Vector3)(ref val))..ctor(x, 0.085f, 0.62f);
			tool.Beam("finewood", start, val, 0.105f, 0.106f);
			tool.Beam("wood", val, new Vector3(x, -0.437f, 0.91f), 0.125f, 0.12f);
			tool.Box("bronze", x, -0.24f, 0.775f, 0.164f, 0.067f, 0.172f, new Vector3(23f, 0f, 0f));
			tool.Box("bronze", x, -0.48f, 0.91f, 0.16f, 0.15f, 0.18f, new Vector3(22f, 0f, 0f));
			tool.Bearing(x, -0.505f, 1.03f, 0.111f, 0.08f, "iron");
			tool.Bearing(x + 0.046f, -0.505f, 1.03f, 0.047f, 0.021f);
		}

		private static void AddCutter(WagonModel tool, float x)
		{
			//IL_0011: 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_0057: 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_00a5: 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)
			tool.Beam("rawwood", new Vector3(x, -0.035f, 0.56f), new Vector3(x, -0.43f, 1.03f), 0.14f, 0.135f);
			tool.Box("wood", x, -0.459f, 1.04f, 1.8f, 0.12f, 0.31f);
			for (int i = -3; i <= 3; i++)
			{
				float x2 = x + (float)i * 0.25f;
				tool.Box("iron", x2, -0.49f, 1.24f, 0.091f, 0.067f, 0.28f, new Vector3(-13f, 0f, 0f));
				tool.Box("bronze", x2, -0.513f, 1.385f, 0.058f, 0.048f, 0.12f, new Vector3(-15f, 0f, 0f));
			}
		}

		private static void AddHopper(ObjectDB db, Transform art)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: 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_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0235: Unknown result type (might be due to invalid IL or missing references)
			WagonModel wagonModel = new WagonModel(db, art);
			wagonModel.Box("rawwood", 0f, 1.002f, 0.47f, 1.16f, 0.17f, 0.57f);
			for (int i = -1; i <= 1; i += 2)
			{
				wagonModel.Box("finewood", (float)i * 0.66f, 1.2f, 0.47f, 0.115f, 0.43f, 0.85f, new Vector3(0f, 0f, (float)(-i * 16)));
				wagonModel.Box("wood", 0f, 1.21f, 0.47f + (float)i * 0.389f, 1.37f, 0.44f, 0.108f, new Vector3((float)(i * 14), 0f, 0f));
				wagonModel.Box("wood", (float)i * 0.74f, 1.424f, 0.47f, 0.1f, 0.092f, 0.96f);
			}
			for (int j = -3; j <= 3; j++)
			{
				wagonModel.Box((j % 2 == 0) ? "wood" : "rawwood", (float)j * 0.198f, 1.47f, 0.47f, 0.196f, 0.07f, 0.94f);
			}
			for (int k = -1; k <= 1; k += 2)
			{
				wagonModel.Box("bronze", (float)k * 0.48f, 1.513f, 0.47f, 0.078f, 0.028f, 0.94f);
				wagonModel.Box("bronze", (float)k * 0.48f, 1.34f, 0.89f, 0.078f, 0.29f, 0.025f);
				wagonModel.Cylinder("iron", (float)k * 0.48f, 1.533f, 0.77f, 0.028f, 0.019f);
			}
			wagonModel.Box("bronze", 0f, 1.54f, 0.75f, 0.205f, 0.05f, 0.087f);
			wagonModel.Commit();
		}

		private static Transform AddReel(ObjectDB db, Transform fixture)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: 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_00d8: 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_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			Transform val = WagonModel.Child(fixture, "Harvest reel", new Vector3(0f, -0.087f, 0.96f));
			WagonModel wagonModel = new WagonModel(db, val);
			float num = HalfSpan * 2f + 1.57f;
			wagonModel.Bearing(0f, 0f, 0f, 0.073f, num + 0.11f, "iron");
			for (int i = 0; i <= 6; i++)
			{
				float num2 = ((float)i - 3f) * (num - 0.11f) / 6f;
				wagonModel.Bearing(num2, 0f, 0f, 0.133f, 0.127f);
				for (int j = 0; j < 4; j++)
				{
					Vector3 val2 = Quaternion.Euler((float)(j * 90), 0f, 0f) * Vector3.up;
					wagonModel.Beam("wood", new Vector3(num2, 0f, 0f), new Vector3(num2, 0f, 0f) + val2 * 0.27f, 0.093f, 0.116f);
				}
			}
			for (int k = 0; k < 4; k++)
			{
				Vector3 val3 = Quaternion.Euler((float)(k * 90), 0f, 0f) * Vector3.up * 0.269f;
				wagonModel.Box((k % 2 == 0) ? "rawwood" : "wood", 0f, val3.y, val3.z, num, 0.069f, 0.092f, new Vector3((float)(k * 90), 0f, 0f));
			}
			wagonModel.Commit();
			return val;
		}
	}
	internal enum FarmImplement
	{
		Plow,
		Sower,
		Combine
	}
	internal static class FarmWagons
	{
		private sealed class Design(string id, string name, string description, int bronze, int wood, int nails, int capacity)
		{
			internal readonly string Id = id;

			internal readonly string Name = name;

			internal readonly string Description = description;

			internal readonly int Bronze = bronze;

			internal readonly int Wood = wood;

			internal readonly int Nails = nails;

			internal readonly int Capacity = capacity;

			internal GameObject Prefab;
		}

		internal const int Rows = 6;

		internal const float RowSpacing = 1.8f;

		internal const string DeployedKey = "fred.fieldartillery.farm.deployed";

		private static readonly Design[] Designs = new Design[3]
		{
			new Design("fred_bronze_plow", "Bronze Plow", "Cuts six parallel rows of cultivated soil.", 8, 28, 12, 0),
			new Design("fred_bronze_sower", "Bronze Seed Wagon", "Plants six rows from the first seed stack in its hopper.", 10, 32, 16, 24),
			new Design("fred_bronze_combine", "Bronze Combine Wagon", "Harvests mature field crops directly into its cargo.", 14, 38, 20, 48)
		};

		private static Transform prefabs;

		private static IModHost host;

		internal static void Apply(string id, IModHost modHost)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Expected O, but got Unknown
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Expected O, but got Unknown
			host = modHost;
			Harmony harmony = new Harmony(id);
			Patch(typeof(ObjectDB), "CopyOtherDB", "Register");
			Patch(typeof(ObjectDB), "Awake", "Register");
			Patch(typeof(ZNetScene), "Awake", "RegisterScene");
			Patch(typeof(Vagon), "GetHoverText", "HitchPrompt");
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "SetControls", (Type[])null, (Type[])null), new HarmonyMethod(typeof(FarmWagons), "ToggleWork", (Type[])null)
			{
				priority = 800
			}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Vagon), "Interact", (Type[])null, (Type[])null), new HarmonyMethod(typeof(FarmWagons), "CheckTowAccess", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			FarmCrops.Apply(harmony);
			FarmSoil.Apply(harmony);
			void Patch(Type type, string target, string method)
			{
				//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_0030: Expected O, but got Unknown
				harmony.Patch((MethodBase)AccessTools.Method(type, target, (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(FarmWagons), method, (Type[])null)
				{
					priority = 0
				}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		private static void RegisterScene()
		{
			if ((Object)(object)ObjectDB.instance != (Object)null)
			{
				Register(ObjectDB.instance);
			}
		}

		private static void Register(ObjectDB __instance)
		{
			//IL_00b8: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Expected O, but got Unknown
			ZNetScene instance = ZNetScene.instance;
			GameObject itemPrefab = __instance.GetItemPrefab("Hammer");
			List<GameObject> list = ((itemPrefab == null) ? null : itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared.m_buildPieces?.m_pieces);
			GameObject val = (((Object)(object)instance == (Object)null) ? null : instance.GetPrefab("Cart"));
			if ((Object)(object)val == (Object)null || list == null)
			{
				return;
			}
			GameObject? obj = list.Find((GameObject piece) => ((Object)piece).name == "forge");
			CraftingStation val2 = ((obj != null) ? obj.GetComponent<CraftingStation>() : null);
			if ((Object)(object)val2 == (Object)null)
			{
				return;
			}
			FarmCrops.ReadCultivator(__instance);
			FarmSoil.ReadCultivator(__instance);
			if ((Object)(object)prefabs == (Object)null)
			{
				GameObject val3 = new GameObject("Bronze farming wagon prefabs");
				val3.SetActive(false);
				Object.DontDestroyOnLoad((Object)val3);
				prefabs = val3.transform;
			}
			for (int num = 0; num < Designs.Length; num++)
			{
				Design design = Designs[num];
				if ((Object)(object)design.Prefab == (Object)null)
				{
					design.Prefab = Build(__instance, val, val2, (FarmImplement)num, design);
				}
				if (!list.Contains(design.Prefab))
				{
					list.Add(design.Prefab);
				}
				if ((Object)(object)instance.GetPrefab(design.id) != (Object)(object)design.Prefab)
				{
					host.AddPrefabs(instance, (IEnumerable<GameObject>)(object)new GameObject[1] { design.Prefab });
				}
			}
		}

		private static GameObject Build(ObjectDB db, GameObject stock, CraftingStation station, FarmImplement implement, Design design)
		{
			//IL_0044: 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_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = Object.Instantiate<GameObject>(stock, prefabs, false);
			((Object)val).name = design.id;
			Piece component = val.GetComponent<Piece>();
			component.m_name = design.name;
			component.m_description = design.description;
			component.m_category = (PieceCategory)1;
			component.m_craftingStation = station;
			component.m_resources = (Requirement[])(object)new Requirement[3]
			{
				Cost("Bronze", design.bronze),
				Cost("Wood", design.wood),
				Cost("BronzeNails", design.nails)
			};
			Vagon component2 = val.GetComponent<Vagon>();
			component2.m_name = design.name;
			component2.m_baseMass = 14f;
			component2.m_itemWeightMassFactor = 0.065f;
			component2.m_playerExtraPullMass = 0f;
			component2.m_detachDistance = 3.7f;
			component2.m_attachPoint.localPosition = new Vector3(component2.m_attachPoint.localPosition.x, 0.68f, component2.m_attachPoint.localPosition.z);
			foreach (LoadData loadVi in component2.m_loadVis)
			{
				if ((Object)(object)loadVi.m_gameobject != (Object)null)
				{
					loadVi.m_gameobject.SetActive(false);
				}
			}
			component2.m_loadVis.Clear();
			if (design.capacity == 0)
			{
				Object.Destroy((Object)(object)component2.m_container);
				component2.m_container = null;
			}
			else
			{
				component2.m_container.m_width = 8;
				component2.m_container.m_height = design.capacity / 8;
				component2.m_container.m_name = ((implement == FarmImplement.Sower) ? "Seed Hopper" : "Harvest Cargo");
			}
			FarmWagonArt.Build(db, component2, implement, out var fixture, out var reel);
			val.AddComponent<CarriageControls>();
			val.AddComponent<FarmWagon>().Configure(implement, fixture, reel);
			return val;
			Requirement Cost(string item, int count)
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//IL_0005: Unknown result type (might be due to invalid IL or missing references)
				//IL_001c: 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_002b: Expected O, but got Unknown
				return new Requirement
				{
					m_resItem = db.GetItemPrefab(item).GetComponent<ItemDrop>(),
					m_amount = count,
					m_recover = true
				};
			}
		}

		internal static bool VerifyWorkingWagon(long sender, ZDOID id, FarmImplement implement, Vector3 target)
		{
			//IL_0005: 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_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)
			ZDO zDO = ZDOMan.instance.GetZDO(id);
			if (zDO != null && zDO.GetOwner() == sender && zDO.GetPrefab() == StringExtensionMethods.GetStableHashCode(Designs[(int)implement].id))
			{
				Vector3 val = zDO.GetPosition() - target;
				return ((Vector3)(ref val)).sqrMagnitude <= 400f;
			}
			return false;
		}

		internal static bool Access(Vector3 position, float radius = 0f)
		{
			//IL_0000: 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)
			if (!Location.IsInsideNoBuildLocation(position))
			{
				return PrivateArea.CheckAccess(position, radius, false, false);
			}
			return false;
		}

		private static bool CheckTowAccess(Vagon __instance, Humanoid character, bool hold)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			if (!hold && !((Object)(object)((Component)__instance).GetComponent<FarmWagon>() == (Object)null) && !((Object)(object)character != (Object)(object)Player.m_localPlayer))
			{
				return Access(((Component)__instance).transform.position);
			}
			return true;
		}

		private static void HitchPrompt(Vagon __instance, ref string __result)
		{
			FarmWagon component = ((Component)__instance).GetComponent<FarmWagon>();
			if (!((Object)(object)component == (Object)null))
			{
				bool flag = (Object)(object)Player.m_localPlayer != (Object)null && (Object)(object)CarriageControls.CurrentTow(Player.m_localPlayer) == (Object)(object)__instance;
				__result = Localization.inst