Decompiled source of BetterHoe v0.2.0

BetterHoe.dll

Decompiled 6 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BetterHoe.Core;
using BetterHoe.Equip;
using BetterHoe.Grid;
using BetterHoe.History;
using BetterHoe.Pieces;
using BetterHoe.Terrain;
using BetterHoe.UI;
using HarmonyLib;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("BetterHoe")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Shape terrain on a grid with the hoe: level, raise, lower, paint and reset exact areas, build smooth ramps, and take out a hoe with one key.")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyInformationalVersion("0.2.0+7f980707989c314d997c04c6fd7060a71e2a481c")]
[assembly: AssemblyProduct("BetterHoe")]
[assembly: AssemblyTitle("BetterHoe")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[DebuggerDisplay("Count = {System.Collections.ICollection.Count}")]
[CompilerGenerated]
internal sealed class <>z__ReadOnlyArray<T> : IEnumerable, ICollection, IList, IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>, IList<T>
{
	int ICollection.Count => _items.Length;

	bool ICollection.IsSynchronized => false;

	object ICollection.SyncRoot => this;

	object IList.this[int index]
	{
		get
		{
			return _items[index];
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	bool IList.IsFixedSize => true;

	bool IList.IsReadOnly => true;

	int IReadOnlyCollection<T>.Count => _items.Length;

	T IReadOnlyList<T>.this[int index] => _items[index];

	int ICollection<T>.Count => _items.Length;

	bool ICollection<T>.IsReadOnly => true;

	T IList<T>.this[int index]
	{
		get
		{
			return _items[index];
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	public <>z__ReadOnlyArray(T[] items)
	{
		_items = items;
	}

	IEnumerator IEnumerable.GetEnumerator()
	{
		return ((IEnumerable)_items).GetEnumerator();
	}

	void ICollection.CopyTo(Array array, int index)
	{
		((ICollection)_items).CopyTo(array, index);
	}

	int IList.Add(object value)
	{
		throw new NotSupportedException();
	}

	void IList.Clear()
	{
		throw new NotSupportedException();
	}

	bool IList.Contains(object value)
	{
		return ((IList)_items).Contains(value);
	}

	int IList.IndexOf(object value)
	{
		return ((IList)_items).IndexOf(value);
	}

	void IList.Insert(int index, object value)
	{
		throw new NotSupportedException();
	}

	void IList.Remove(object value)
	{
		throw new NotSupportedException();
	}

	void IList.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}

	IEnumerator<T> IEnumerable<T>.GetEnumerator()
	{
		return ((IEnumerable<T>)_items).GetEnumerator();
	}

	void ICollection<T>.Add(T item)
	{
		throw new NotSupportedException();
	}

	void ICollection<T>.Clear()
	{
		throw new NotSupportedException();
	}

	bool ICollection<T>.Contains(T item)
	{
		return ((ICollection<T>)_items).Contains(item);
	}

	void ICollection<T>.CopyTo(T[] array, int arrayIndex)
	{
		((ICollection<T>)_items).CopyTo(array, arrayIndex);
	}

	bool ICollection<T>.Remove(T item)
	{
		throw new NotSupportedException();
	}

	int IList<T>.IndexOf(T item)
	{
		return ((IList<T>)_items).IndexOf(item);
	}

	void IList<T>.Insert(int index, T item)
	{
		throw new NotSupportedException();
	}

	void IList<T>.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}
}
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 BetterHoe
{
	[BepInPlugin("javidahmed64592.betterhoe", "Better Hoe", "0.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class BetterHoePlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "javidahmed64592.betterhoe";

		public const string PluginName = "Better Hoe";

		public const string PluginVersion = "0.2.0";

		private Harmony _harmony;

		private void Awake()
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			PhantomHoe.BindConfig(((BaseUnityPlugin)this).Config);
			AreaTool.BindConfig(((BaseUnityPlugin)this).Config);
			UndoHistory.BindConfig(((BaseUnityPlugin)this).Config);
			GridOverlay.BindConfig(((BaseUnityPlugin)this).Config);
			Translations.Load();
			HoePieces.Register();
			_harmony = new Harmony("javidahmed64592.betterhoe");
			_harmony.PatchAll();
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			AreaTool.Destroy();
		}

		private void Update()
		{
			Player localPlayer = Player.m_localPlayer;
			PhantomHoe.Update(localPlayer);
			AreaTool.Update(localPlayer);
		}
	}
}
namespace BetterHoe.UI
{
	internal static class HoeKeyHints
	{
		private sealed class State
		{
			public Player Player;

			public Piece Piece;

			public HoeTool TerrainTool;

			public HoeTool AreaTool;
		}

		private sealed class Hint
		{
			public string[] Keys;

			public Func<State, string> Label;

			public CanvasGroup Group;

			public TextMeshProUGUI LabelText;

			public string ShownLabel;
		}

		private sealed class VanillaHint
		{
			public GameObject Item;

			public TextMeshProUGUI Label;

			public string OriginalText;

			public bool Changed;
		}

		private const string ContainerName = "BetterHoe_KeyHints";

		private const string ScrollIcon = "@scroll";

		private const string UndoKey = "@undo";

		private const string RedoKey = "@redo";

		private const float LabelFontSize = 16f;

		private const float IconHeight = 32f;

		private const float IconScale = 1.8f;

		private const float RowSpacing = 16f;

		private static readonly List<Hint> Hints = new List<Hint>(11)
		{
			new Hint
			{
				Keys = new string[1] { "@undo" },
				Label = (State _) => (!UndoHistory.CanUndo) ? null : "$betterhoe_hint_undo"
			},
			new Hint
			{
				Keys = new string[1] { "@redo" },
				Label = (State _) => (!UndoHistory.CanRedo) ? null : "$betterhoe_hint_redo"
			},
			new Hint
			{
				Keys = new string[2] { "Left Ctrl", "@scroll" },
				Label = (State s) => (s.AreaTool == null) ? null : "$betterhoe_hint_width"
			},
			new Hint
			{
				Keys = new string[2] { "Left Shift", "@scroll" },
				Label = (State s) => (s.AreaTool == null || s.AreaTool.Shape == ShapeMode.Ramp) ? null : "$betterhoe_hint_length"
			},
			new Hint
			{
				Keys = new string[2] { "Left Alt", "@scroll" },
				Label = delegate(State s)
				{
					switch (s.AreaTool?.Shape)
					{
					case ShapeMode.Raise:
					case ShapeMode.Lower:
						return "$betterhoe_hint_step";
					case ShapeMode.Ramp:
						return "$betterhoe_hint_blend";
					default:
						return (string)null;
					}
				}
			},
			new Hint
			{
				Keys = new string[2] { "Left Ctrl", "$KEY_BuildMenu" },
				Label = (State s) => (!AreaTool.GridToggleApplies(s.Player)) ? null : "$betterhoe_hint_grid"
			},
			new Hint
			{
				Keys = new string[1] { "$KEY_AltPlace" },
				Label = (State s) => (s.AreaTool != null || s.TerrainTool == null || !s.Piece.m_allowAltGroundPlacement) ? null : "$betterhoe_hint_altplace"
			},
			new Hint
			{
				Keys = new string[1] { "@scroll" },
				Label = (State s) => (s.TerrainTool != null) ? null : "$hud_rotate"
			},
			new Hint
			{
				Keys = new string[1] { "$KEY_Remove" },
				Label = LockLabel
			},
			new Hint
			{
				Keys = new string[1] { "$KEY_BuildMenu" },
				Label = (State _) => (!AreaTool.RampStarted) ? "$betterhoe_hint_menu" : "$betterhoe_hint_ramp_cancel"
			},
			new Hint
			{
				Keys = new string[1] { "$KEY_Attack" },
				Label = PlaceLabel
			}
		};

		private static readonly string[] AllLabels = new string[24]
		{
			"$betterhoe_hint_undo", "$betterhoe_hint_redo", "$betterhoe_hint_width", "$betterhoe_hint_length", "$betterhoe_hint_step", "$betterhoe_hint_blend", "$betterhoe_hint_grid", "$betterhoe_hint_altplace", "$hud_rotate", "$betterhoe_hint_lock",
			"$betterhoe_hint_unlock", "$betterhoe_hint_ramp_cancel", "$betterhoe_hint_menu", "$hud_place", "$betterhoe_hint_ramp_start", "$betterhoe_hint_ramp_end", "$betterhoe_hint_flatten", "$betterhoe_hint_raise", "$betterhoe_hint_lower", "$betterhoe_hint_reset_height",
			"$betterhoe_hint_path", "$betterhoe_hint_paved", "$betterhoe_hint_cultivate", "$betterhoe_hint_reset_paint"
		};

		private static readonly Dictionary<string, VanillaHint> Vanilla = new Dictionary<string, VanillaHint>();

		private static readonly string[] VanillaNames = new string[7] { "Place", "Remove", "BuildMenu", "AltPlace", "Snap", "Copy", "rotate" };

		private static readonly State Current = new State();

		private static GameObject _container;

		private static RectTransform _stack;

		private static RectTransform _keyboard;

		private static bool _createFailed;

		private static bool _layoutDirty;

		private static string LockLabel(State state)
		{
			bool flag;
			switch (state.AreaTool?.Shape)
			{
			case ShapeMode.Flatten:
			case ShapeMode.Ramp:
				flag = true;
				break;
			default:
				flag = false;
				break;
			}
			bool num = flag;
			string result = (AreaTool.HeightLocked ? "$betterhoe_hint_unlock" : "$betterhoe_hint_lock");
			if (!num)
			{
				return null;
			}
			return result;
		}

		private static string PlaceLabel(State state)
		{
			string result = (AreaTool.RampStarted ? "$betterhoe_hint_ramp_end" : "$betterhoe_hint_ramp_start");
			ShapeMode? shapeMode = state.TerrainTool?.Shape;
			if (shapeMode.HasValue)
			{
				if (shapeMode == ShapeMode.Ramp)
				{
					return result;
				}
				return state.TerrainTool.ActionToken;
			}
			return "$hud_place";
		}

		private static void Update(KeyHints keyHints, Player player)
		{
			Piece val = AreaTool.SelectedPiece(player);
			if (!((Object)(object)val != (Object)null) || !((Object)(object)keyHints.m_buildHints != (Object)null) || !keyHints.m_buildHints.activeInHierarchy || ZInput.IsGamepadActive())
			{
				RestoreVanilla();
				SetContainerActive(active: false);
			}
			else
			{
				if ((Object)(object)_container == (Object)null && (_createFailed || !Create(keyHints)))
				{
					return;
				}
				Current.Player = player;
				Current.Piece = val;
				Current.TerrainTool = AreaTool.SelectedTerrainTool(player);
				Current.AreaTool = AreaTool.ActiveTool(player);
				if (Hud.IsPieceSelectionVisible())
				{
					UpdateVanillaForMenu();
					SetContainerActive(active: false);
					return;
				}
				string[] vanillaNames = VanillaNames;
				for (int i = 0; i < vanillaNames.Length; i++)
				{
					SetVanilla(vanillaNames[i], active: false, null);
				}
				foreach (Hint hint in Hints)
				{
					string text = hint.Label(Current);
					hint.Group.alpha = ((text != null) ? 1f : 0f);
					if (text != null && text != hint.ShownLabel)
					{
						hint.ShownLabel = text;
						((TMP_Text)hint.LabelText).text = Localization.instance.Localize(text);
					}
				}
				SetContainerActive(active: true);
				if (_layoutDirty)
				{
					LayoutRebuilder.ForceRebuildLayoutImmediate(_stack);
					_layoutDirty = false;
				}
				Align();
			}
		}

		private static void UpdateVanillaForMenu()
		{
			SetVanilla("Place", active: true, PlaceLabel(Current));
			SetVanilla("Remove", active: false, null);
			SetVanilla("BuildMenu", active: true, "$betterhoe_hint_menu");
			SetVanilla("AltPlace", active: false, null);
			SetVanilla("Snap", active: false, null);
			SetVanilla("Copy", active: false, null);
			SetVanilla("rotate", active: false, null);
		}

		private static void SetVanilla(string name, bool active, string label)
		{
			if (!Vanilla.TryGetValue(name, out var value) || (Object)(object)value.Item == (Object)null)
			{
				return;
			}
			if (value.Item.activeSelf != active)
			{
				value.Item.SetActive(active);
				value.Changed = true;
			}
			if (!((Object)(object)value.Label == (Object)null) && active)
			{
				string text = ((label != null) ? Localization.instance.Localize(label) : value.OriginalText);
				if (((TMP_Text)value.Label).text != text)
				{
					((TMP_Text)value.Label).text = text;
					value.Changed = true;
				}
			}
		}

		private static void RestoreVanilla()
		{
			foreach (VanillaHint value in Vanilla.Values)
			{
				if (value.Changed && !((Object)(object)value.Item == (Object)null))
				{
					value.Item.SetActive(true);
					if ((Object)(object)value.Label != (Object)null)
					{
						((TMP_Text)value.Label).text = value.OriginalText;
					}
					value.Changed = false;
				}
			}
		}

		private static void SetContainerActive(bool active)
		{
			if ((Object)(object)_container != (Object)null && _container.activeSelf != active)
			{
				_container.SetActive(active);
				_layoutDirty |= active;
			}
		}

		private static void Align()
		{
			//IL_003e: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
			Vector3[] array = (Vector3[])(object)new Vector3[4];
			_keyboard.GetWorldCorners(array);
			HorizontalLayoutGroup component = ((Component)_keyboard).GetComponent<HorizontalLayoutGroup>();
			float num = (((Object)(object)component != (Object)null) ? ((float)((LayoutGroup)component).padding.right * ((Transform)_keyboard).lossyScale.x) : 0f);
			float num2 = (((Object)(object)component != (Object)null) ? ((float)((LayoutGroup)component).padding.bottom * ((Transform)_keyboard).lossyScale.y) : 0f);
			((Transform)_stack).position = new Vector3(array[3].x - num, array[3].y + num2, array[3].z);
		}

		private static bool Create(KeyHints keyHints)
		{
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b1: Expected O, but got Unknown
			Transform val = keyHints.m_buildHints.transform.Find("Keyboard");
			Transform val2 = (((Object)(object)val != (Object)null) ? val.Find("Copy") : null);
			Transform val3 = (((Object)(object)val != (Object)null) ? val.Find("rotate") : null);
			Transform val4 = (((Object)(object)val2 != (Object)null) ? val2.Find("Text") : null);
			Transform val5 = (((Object)(object)val2 != (Object)null) ? val2.Find("Text (1)") : null);
			Transform val6 = (((Object)(object)val2 != (Object)null) ? val2.Find("key_bkg") : null);
			Transform val7 = (((Object)(object)val3 != (Object)null) ? val3.Find("mousew_icon") : null);
			HorizontalLayoutGroup val8 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponent<HorizontalLayoutGroup>() : null);
			if ((Object)(object)val4 == (Object)null || (Object)(object)val5 == (Object)null || (Object)(object)val6 == (Object)null || (Object)(object)val7 == (Object)null || (Object)(object)val8 == (Object)null)
			{
				_createFailed = true;
				Logger.LogWarning((object)"Vanilla build key hints layout not as expected, not changing the hoe's key hints.");
				return false;
			}
			Vanilla.Clear();
			string[] vanillaNames = VanillaNames;
			foreach (string text in vanillaNames)
			{
				Transform val9 = val.Find(text);
				if (!((Object)(object)val9 == (Object)null))
				{
					Transform obj = val9.Find("Text");
					TextMeshProUGUI val10 = ((obj != null) ? ((Component)obj).GetComponent<TextMeshProUGUI>() : null);
					Vanilla[text] = new VanillaHint
					{
						Item = ((Component)val9).gameObject,
						Label = val10,
						OriginalText = (((Object)(object)val10 != (Object)null) ? ((TMP_Text)val10).text : null)
					};
				}
			}
			RectTransform component = new GameObject("BetterHoe_KeyHints", new Type[1] { typeof(RectTransform) }).GetComponent<RectTransform>();
			((Transform)component).SetParent(keyHints.m_buildHints.transform, false);
			RectTransform val11 = CreateStack(component);
			string text2 = LongestLabel();
			foreach (Hint hint in Hints)
			{
				GameObject val12 = Object.Instantiate<GameObject>(((Component)val2).gameObject, (Transform)(object)val11, false);
				((Object)val12).name = "BetterHoe_Hint";
				val12.SetActive(true);
				for (int num = val12.transform.childCount - 1; num >= 0; num--)
				{
					Object.DestroyImmediate((Object)(object)((Component)val12.transform.GetChild(num)).gameObject);
				}
				hint.LabelText = AddText(val4, val12.transform, text2);
				((TMP_Text)hint.LabelText).alignment = (TextAlignmentOptions)516;
				hint.ShownLabel = null;
				Transform val13 = CreateKeyGroup(val12.transform, val8);
				string[] array = ExpandShortcuts(hint.Keys);
				for (int j = 0; j < array.Length; j++)
				{
					if (j > 0)
					{
						AddText(val5, val13, "+");
					}
					if (array[j] == "@scroll")
					{
						AddScrollIcon(val7, val13);
						continue;
					}
					GameObject obj2 = Object.Instantiate<GameObject>(((Component)val6).gameObject, val13, false);
					obj2.SetActive(true);
					((TMP_Text)obj2.GetComponentInChildren<TextMeshProUGUI>()).text = Localization.instance.Localize(array[j]);
				}
				hint.Group = val12.GetComponent<CanvasGroup>() ?? val12.AddComponent<CanvasGroup>();
			}
			LayoutRebuilder.ForceRebuildLayoutImmediate(val11);
			EqualiseWidths();
			foreach (Hint hint2 in Hints)
			{
				((TMP_Text)hint2.LabelText).text = "";
			}
			_container = ((Component)component).gameObject;
			_stack = val11;
			_keyboard = (RectTransform)val;
			_layoutDirty = true;
			return true;
		}

		private static string LongestLabel()
		{
			string text = "";
			string[] allLabels = AllLabels;
			foreach (string text2 in allLabels)
			{
				string text3 = Localization.instance.Localize(text2);
				if (text3.Length > text.Length)
				{
					text = text3;
				}
			}
			return text;
		}

		private static RectTransform CreateStack(RectTransform parent)
		{
			//IL_0018: 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)
			RectTransform component = new GameObject("BetterHoe_KeyHints_Stack", new Type[1] { typeof(RectTransform) }).GetComponent<RectTransform>();
			((Transform)component).SetParent((Transform)(object)parent, false);
			component.pivot = new Vector2(1f, 0f);
			VerticalLayoutGroup obj = ((Component)component).gameObject.AddComponent<VerticalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)obj).spacing = 16f;
			((LayoutGroup)obj).childAlignment = (TextAnchor)8;
			((HorizontalOrVerticalLayoutGroup)obj).childControlWidth = true;
			((HorizontalOrVerticalLayoutGroup)obj).childControlHeight = true;
			((HorizontalOrVerticalLayoutGroup)obj).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)obj).childForceExpandHeight = false;
			ContentSizeFitter obj2 = ((Component)component).gameObject.AddComponent<ContentSizeFitter>();
			obj2.horizontalFit = (FitMode)2;
			obj2.verticalFit = (FitMode)2;
			return component;
		}

		private static Transform CreateKeyGroup(Transform row, HorizontalLayoutGroup template)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("Keys", new Type[1] { typeof(RectTransform) });
			val.transform.SetParent(row, false);
			HorizontalLayoutGroup obj = val.AddComponent<HorizontalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)obj).spacing = ((HorizontalOrVerticalLayoutGroup)template).spacing;
			((LayoutGroup)obj).childAlignment = (TextAnchor)3;
			((HorizontalOrVerticalLayoutGroup)obj).childControlWidth = ((HorizontalOrVerticalLayoutGroup)template).childControlWidth;
			((HorizontalOrVerticalLayoutGroup)obj).childControlHeight = ((HorizontalOrVerticalLayoutGroup)template).childControlHeight;
			((HorizontalOrVerticalLayoutGroup)obj).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)obj).childForceExpandHeight = false;
			((RectTransform)val.transform).sizeDelta = new Vector2(0f, 32f);
			LayoutElement obj2 = val.AddComponent<LayoutElement>();
			float minHeight = (obj2.preferredHeight = 32f);
			obj2.minHeight = minHeight;
			return val.transform;
		}

		private static void AddScrollIcon(Transform template, Transform parent)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = Object.Instantiate<GameObject>(((Component)template).gameObject, parent, false);
			val.SetActive(true);
			Image component = val.GetComponent<Image>();
			component.preserveAspect = true;
			float num;
			if (!((Object)(object)component.sprite != (Object)null))
			{
				num = 0.6f;
			}
			else
			{
				Rect rect = component.sprite.rect;
				float width = ((Rect)(ref rect)).width;
				rect = component.sprite.rect;
				num = width / ((Rect)(ref rect)).height;
			}
			float num2 = num;
			LayoutElement obj = val.GetComponent<LayoutElement>() ?? val.AddComponent<LayoutElement>();
			float minWidth = (obj.preferredWidth = 32f * num2 * 1.8f);
			obj.minWidth = minWidth;
			minWidth = (obj.preferredHeight = 32f);
			obj.minHeight = minWidth;
			minWidth = (obj.flexibleHeight = 0f);
			obj.flexibleWidth = minWidth;
			val.transform.localScale = new Vector3(1.8f, 1.8f, 1f);
		}

		private static string[] ExpandShortcuts(string[] keys)
		{
			//IL_004a: 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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: 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_0091: Unknown result type (might be due to invalid IL or missing references)
			List<string> list = new List<string>();
			foreach (string text in keys)
			{
				if (!(text == "@undo") && !(text == "@redo"))
				{
					list.Add(text);
					continue;
				}
				KeyboardShortcut val = ((text == "@undo") ? UndoHistory.UndoKey : UndoHistory.RedoKey);
				foreach (KeyCode modifier in ((KeyboardShortcut)(ref val)).Modifiers)
				{
					list.Add(KeyName(modifier));
				}
				list.Add(KeyName(((KeyboardShortcut)(ref val)).MainKey));
			}
			return list.ToArray();
		}

		private unsafe static string KeyName(KeyCode key)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected I4, but got Unknown
			return (key - 303) switch
			{
				3 => "Left Ctrl", 
				2 => "Right Ctrl", 
				1 => "Left Shift", 
				0 => "Right Shift", 
				5 => "Left Alt", 
				4 => "Right Alt", 
				_ => ((object)(*(KeyCode*)(&key))/*cast due to .constrained prefix*/).ToString(), 
			};
		}

		private static void EqualiseWidths()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected O, but got Unknown
			for (int i = 0; i < 2; i++)
			{
				float num = 0f;
				foreach (Hint hint in Hints)
				{
					num = Mathf.Max(num, LayoutUtility.GetPreferredWidth((RectTransform)((Component)hint.Group).transform.GetChild(i)));
				}
				foreach (Hint hint2 in Hints)
				{
					GameObject gameObject = ((Component)((Component)hint2.Group).transform.GetChild(i)).gameObject;
					LayoutElement obj = gameObject.GetComponent<LayoutElement>() ?? gameObject.AddComponent<LayoutElement>();
					obj.minWidth = num;
					obj.preferredWidth = num;
				}
			}
		}

		private static TextMeshProUGUI AddText(Transform template, Transform parent, string text)
		{
			GameObject obj = Object.Instantiate<GameObject>(((Component)template).gameObject, parent, false);
			obj.SetActive(true);
			LayoutElement component = obj.GetComponent<LayoutElement>();
			if ((Object)(object)component != (Object)null)
			{
				Object.DestroyImmediate((Object)(object)component);
			}
			TextMeshProUGUI component2 = obj.GetComponent<TextMeshProUGUI>();
			((TMP_Text)component2).enableAutoSizing = false;
			((TMP_Text)component2).fontSize = 16f;
			((TMP_Text)component2).textWrappingMode = (TextWrappingModes)0;
			((TMP_Text)component2).text = text;
			return component2;
		}

		internal static void AfterUpdateHints(KeyHints keyHints)
		{
			if (_container != null && (Object)(object)_container == (Object)null)
			{
				Reset();
			}
			Update(keyHints, Player.m_localPlayer);
		}

		private static void Reset()
		{
			_container = null;
			_stack = null;
			_keyboard = null;
			_createFailed = false;
			Vanilla.Clear();
			foreach (Hint hint in Hints)
			{
				hint.Group = null;
				hint.LabelText = null;
				hint.ShownLabel = null;
			}
		}
	}
	internal static class PieceMenuTidy
	{
		private const string HoeTableName = "_HoePieceTable";

		private const string TitleName = "BetterHoe_MenuTitle";

		private static readonly Color TitleColor = new Color(1f, 0.72f, 0.36f);

		private static TextMeshProUGUI _title;

		internal static void AfterUpdateTagButtons(BuildUi buildUi)
		{
			RectTransform tabContainer = buildUi.m_tabContainer;
			PieceTable currentBuildTool = buildUi.m_currentBuildTool;
			if (!((Object)(object)tabContainer == (Object)null) && !((Object)(object)((Transform)tabContainer).parent == (Object)null) && !((Object)(object)currentBuildTool == (Object)null))
			{
				bool flag = !currentBuildTool.m_hideAdvancedMenu;
				bool flag2 = !flag && ((Object)currentBuildTool).name == "_HoePieceTable";
				SetActive(((Transform)tabContainer).parent.Find("InputHelp"), flag);
				SetActive(((Transform)tabContainer).parent.Find("TabBorder"), flag || flag2);
				if (flag2 && (Object)(object)_title == (Object)null)
				{
					_title = CreateTitle(tabContainer);
				}
				if ((Object)(object)_title != (Object)null && ((Component)_title).gameObject.activeSelf != flag2)
				{
					((Component)_title).gameObject.SetActive(flag2);
				}
			}
		}

		private static void SetActive(Transform item, bool active)
		{
			if ((Object)(object)item != (Object)null && ((Component)item).gameObject.activeSelf != active)
			{
				((Component)item).gameObject.SetActive(active);
			}
		}

		private static TextMeshProUGUI CreateTitle(RectTransform tabs)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: 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_00ba: Unknown result type (might be due to invalid IL or missing references)
			TextMeshProUGUI componentInChildren = ((Component)tabs).GetComponentInChildren<TextMeshProUGUI>(true);
			if ((Object)(object)componentInChildren == (Object)null)
			{
				return null;
			}
			RectTransform component = new GameObject("BetterHoe_MenuTitle", new Type[1] { typeof(RectTransform) }).GetComponent<RectTransform>();
			((Transform)component).SetParent(((Transform)tabs).parent, false);
			component.anchorMin = tabs.anchorMin;
			component.anchorMax = tabs.anchorMax;
			component.pivot = tabs.pivot;
			component.anchoredPosition = tabs.anchoredPosition;
			component.sizeDelta = tabs.sizeDelta;
			TextMeshProUGUI obj = ((Component)component).gameObject.AddComponent<TextMeshProUGUI>();
			((TMP_Text)obj).font = ((TMP_Text)componentInChildren).font;
			((TMP_Text)obj).fontSharedMaterial = ((TMP_Text)componentInChildren).fontSharedMaterial;
			((TMP_Text)obj).fontSize = ((TMP_Text)componentInChildren).fontSize * 1.25f;
			((TMP_Text)obj).enableAutoSizing = false;
			((Graphic)obj).color = TitleColor;
			((TMP_Text)obj).alignment = (TextAlignmentOptions)514;
			((TMP_Text)obj).textWrappingMode = (TextWrappingModes)0;
			((Graphic)obj).raycastTarget = false;
			((TMP_Text)obj).text = Localization.instance.Localize("$betterhoe_menu_title");
			return obj;
		}
	}
	internal sealed class Readout
	{
		private static readonly Vector2 Offset = new Vector2(0f, -70f);

		private GameObject _object;

		private Text _text;

		public void Show(string text)
		{
			if (Ensure())
			{
				if (_text.text != text)
				{
					_text.text = text;
				}
				if (!_object.activeSelf)
				{
					_object.SetActive(true);
				}
			}
		}

		public void Hide()
		{
			if ((Object)(object)_object != (Object)null && _object.activeSelf)
			{
				_object.SetActive(false);
			}
		}

		public void Destroy()
		{
			if ((Object)(object)_object != (Object)null)
			{
				Object.Destroy((Object)(object)_object);
			}
			_object = null;
			_text = null;
		}

		private bool Ensure()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: 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)
			if ((Object)(object)_object != (Object)null)
			{
				return true;
			}
			GameObject customGUIFront = GUIManager.CustomGUIFront;
			if ((Object)(object)customGUIFront == (Object)null || GUIManager.Instance == null)
			{
				return false;
			}
			_object = GUIManager.Instance.CreateText("", customGUIFront.transform, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), Offset, GUIManager.Instance.AveriaSerifBold, 18, Color.white, true, new Color(0f, 0f, 0f, 0.9f), 500f, 90f, false);
			((Object)_object).name = "BetterHoe_Readout";
			_text = _object.GetComponent<Text>();
			_text.alignment = (TextAnchor)1;
			_text.supportRichText = true;
			((Graphic)_text).raycastTarget = false;
			_object.SetActive(false);
			return true;
		}
	}
}
namespace BetterHoe.Terrain
{
	internal readonly struct Area : IEquatable<Area>
	{
		public readonly int MinX;

		public readonly int MinZ;

		public readonly int SizeX;

		public readonly int SizeZ;

		public int MaxX => MinX + SizeX;

		public int MaxZ => MinZ + SizeZ;

		public float CenterX => (float)MinX + (float)SizeX / 2f;

		public float CenterZ => (float)MinZ + (float)SizeZ / 2f;

		public float Radius => Mathf.Sqrt((float)(SizeX * SizeX + SizeZ * SizeZ)) / 2f;

		public Area(int minX, int minZ, int sizeX, int sizeZ)
		{
			MinX = minX;
			MinZ = minZ;
			SizeX = sizeX;
			SizeZ = sizeZ;
		}

		public static Area Around(Vector3 aim, int sizeX, int sizeZ)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			return new Area(Mathf.RoundToInt(aim.x - (float)sizeX / 2f), Mathf.RoundToInt(aim.z - (float)sizeZ / 2f), sizeX, sizeZ);
		}

		public bool Equals(Area other)
		{
			if (MinX == other.MinX && MinZ == other.MinZ && SizeX == other.SizeX)
			{
				return SizeZ == other.SizeZ;
			}
			return false;
		}

		public override bool Equals(object obj)
		{
			if (obj is Area other)
			{
				return Equals(other);
			}
			return false;
		}

		public override int GetHashCode()
		{
			return (((((MinX * 397) ^ MinZ) * 397) ^ SizeX) * 397) ^ SizeZ;
		}
	}
	internal static class AreaTool
	{
		private const string HoeTableName = "_HoePieceTable";

		private static readonly float[] Steps = new float[5] { 0.1f, 0.25f, 0.5f, 1f, 2f };

		private static readonly float[] Blends = new float[6] { 0f, 1f, 2f, 3f, 4f, 6f };

		private static ConfigEntry<bool> _gridEnabled;

		private static ConfigEntry<int> _width;

		private static ConfigEntry<int> _length;

		private static ConfigEntry<float> _step;

		private static ConfigEntry<int> _maxSize;

		private static ConfigEntry<float> _blend;

		private static readonly GridOverlay Overlay = new GridOverlay();

		private static readonly Readout Readout = new Readout();

		private static int _aimFrame = -10;

		private static Vector3 _aimPoint;

		private static bool _aimOnTerrain;

		private static float? _lockedHeight;

		private static Vector3? _rampStart;

		private static readonly List<(int x, int z, float height, float weight)> RampVertices = new List<(int, int, float, float)>();

		private static (Area area, HoeTool tool, float value) _limitKey;

		private static int _limitCount;

		private static int _limitFrame = -100;

		private const int LimitRefreshFrames = 15;

		[CompilerGenerated]
		private static bool <BlocksMenu>k__BackingField;

		private static RampShape _rampLimitKey;

		private static bool _rampLimit;

		private static int _rampLimitFrame = -100;

		internal static bool Active { get; private set; }

		private static int MaxSize => _maxSize.Value;

		private static int Width => Mathf.Clamp(_width.Value, 1, MaxSize);

		private static int Length => Mathf.Clamp(_length.Value, 1, MaxSize);

		private static float Step => _step.Value;

		private static float Blend => _blend.Value;

		internal static bool HeightLocked => _lockedHeight.HasValue;

		internal static bool RampStarted => _rampStart.HasValue;

		internal static bool BlocksMenu
		{
			get
			{
				if (<BlocksMenu>k__BackingField && !Input.GetMouseButton(1))
				{
					<BlocksMenu>k__BackingField = false;
				}
				if (!_rampStart.HasValue)
				{
					return <BlocksMenu>k__BackingField;
				}
				return true;
			}
			[CompilerGenerated]
			private set
			{
				<BlocksMenu>k__BackingField = value;
			}
		}

		private static bool HasAim => Time.frameCount - _aimFrame <= 2;

		public static void BindConfig(ConfigFile config)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Expected O, but got Unknown
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Expected O, but got Unknown
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Expected O, but got Unknown
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Expected O, but got Unknown
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Expected O, but got Unknown
			_gridEnabled = config.Bind<bool>("Area", "Use With Vanilla Tools", true, "Use the area grid with the hoe's vanilla tools (Level ground, Raise ground, Pathen and Paved road). Toggle it in game with Ctrl+Right-Click. BetterHoe's own tools always use it.");
			_width = config.Bind<int>("Area", "Width", 4, new ConfigDescription("Width in metres of the area from west to east. Change it in game with Ctrl+Scroll.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 64), Array.Empty<object>()));
			_length = config.Bind<int>("Area", "Length", 4, new ConfigDescription("Length in metres of the area from south to north. Change it in game with Shift+Scroll.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 64), Array.Empty<object>()));
			_step = config.Bind<float>("Area", "Height Step", 0.5f, new ConfigDescription("Metres that Raise ground and Lower ground move the area per use (0.1, 0.25, 0.5, 1 or 2). Change it in game with Alt+Scroll.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(Steps[0], Steps[Steps.Length - 1]), Array.Empty<object>()));
			_maxSize = config.Bind<int>("Server", "Max Area Size", 16, new ConfigDescription("Largest width or length in metres of the area. (Synced with server)", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 64), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true
			} }));
			_blend = config.Bind<float>("Ramp", "Edge Blend", 2f, new ConfigDescription("Metres on each side of a ramp where the ground eases into the slope (0, 1, 2, 3, 4 or 6). Change it in game with Alt+Scroll while the Ramp tool is selected.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(Blends[0], Blends[Blends.Length - 1]), Array.Empty<object>()));
			if (Array.IndexOf(Blends, _blend.Value) < 0)
			{
				_blend.Value = 2f;
			}
			if (Array.IndexOf(Steps, _step.Value) < 0)
			{
				_step.Value = 0.5f;
			}
		}

		public static void Destroy()
		{
			Overlay.Destroy();
			Readout.Destroy();
		}

		private static Piece SelectedHoePiece(Player player)
		{
			if ((Object)(object)player == (Object)null || !((Character)player).InPlaceMode() || (Object)(object)player.m_buildPieces == (Object)null)
			{
				return null;
			}
			PieceTable val = ((Humanoid)player).GetRightItem()?.m_shared?.m_buildPieces;
			if (!((Object)(object)val == (Object)null) && !((Object)(object)val != (Object)(object)player.m_buildPieces) && (!(((Object)val).name != "_HoePieceTable") || ((Object)val).name.IndexOf("hoe", StringComparison.OrdinalIgnoreCase) >= 0))
			{
				return val.GetSelectedPiece();
			}
			return null;
		}

		private static HoeTool ToolFor(Piece piece, out bool own)
		{
			own = false;
			if ((Object)(object)piece == (Object)null)
			{
				return null;
			}
			switch (Utils.GetPrefabName(((Component)piece).gameObject))
			{
			case "mud_road_v2":
				return HoeTool.Flatten;
			case "raise_v2":
				return HoeTool.Raise;
			case "path_v2":
				return HoeTool.PaintDirt;
			case "paved_road_v2":
				return HoeTool.PaintPaved;
			case "BetterHoe_Lower":
				own = true;
				return HoeTool.Lower;
			case "BetterHoe_Ramp":
				own = true;
				return HoeTool.Ramp;
			case "BetterHoe_ResetHeight":
				own = true;
				return HoeTool.ResetHeight;
			case "BetterHoe_Cultivate":
				own = true;
				return HoeTool.PaintCultivate;
			case "BetterHoe_ResetPaint":
				own = true;
				return HoeTool.PaintReset;
			default:
				return null;
			}
		}

		internal static HoeTool ActiveTool(Player player)
		{
			if ((Object)(object)player == (Object)null || (Object)(object)player != (Object)(object)Player.m_localPlayer)
			{
				return null;
			}
			bool own;
			HoeTool hoeTool = ToolFor(SelectedHoePiece(player), out own);
			if (hoeTool == null || (!own && !_gridEnabled.Value))
			{
				return null;
			}
			return hoeTool;
		}

		internal static bool HoeTerrainToolSelected(Player player)
		{
			bool own;
			if ((Object)(object)player != (Object)null && (Object)(object)player == (Object)(object)Player.m_localPlayer)
			{
				return ToolFor(SelectedHoePiece(player), out own) != null;
			}
			return false;
		}

		internal static Piece SelectedPiece(Player player)
		{
			if (!((Object)(object)player != (Object)null) || !((Object)(object)player == (Object)(object)Player.m_localPlayer))
			{
				return null;
			}
			return SelectedHoePiece(player);
		}

		internal static HoeTool SelectedTerrainTool(Player player)
		{
			bool own;
			return ToolFor(SelectedPiece(player), out own);
		}

		internal static bool GridToggleApplies(Player player)
		{
			if (ToolFor(SelectedPiece(player), out var own) != null)
			{
				return !own;
			}
			return false;
		}

		internal static void SetAim(Vector3 point, bool onTerrain)
		{
			//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)
			_aimFrame = Time.frameCount;
			_aimPoint = point;
			_aimOnTerrain = onTerrain;
		}

		public static void Update(Player player)
		{
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			Active = false;
			if ((Object)(object)player == (Object)null)
			{
				_lockedHeight = null;
				_rampStart = null;
				UndoHistory.Reset();
				Hide();
				return;
			}
			Piece val = SelectedHoePiece(player);
			if ((Object)(object)val == (Object)null || InputBlocked())
			{
				Hide();
				return;
			}
			UndoHistory.UpdateKeys(player, CheckAccess);
			bool own;
			HoeTool hoeTool = ToolFor(val, out own);
			if (hoeTool != HoeTool.Ramp)
			{
				_rampStart = null;
			}
			if (hoeTool != null && !own && Keys.Ctrl && Input.GetMouseButtonDown(1))
			{
				_gridEnabled.Value = !_gridEnabled.Value;
				((Character)player).Message((MessageType)1, _gridEnabled.Value ? "$betterhoe_grid_on" : "$betterhoe_grid_off", 0, (Sprite)null, false);
			}
			if (hoeTool == null || (!own && !_gridEnabled.Value))
			{
				Hide();
				return;
			}
			Active = true;
			HandleInput(player, hoeTool);
			if (!HasAim)
			{
				Hide();
				return;
			}
			if (hoeTool == HoeTool.Ramp)
			{
				ShowRamp();
				return;
			}
			Area area = CurrentArea();
			float value = ToolValue(hoeTool);
			Overlay.Show(_aimPoint, area, hoeTool, value);
			Readout.Show(ReadoutText(hoeTool, area, value));
		}

		private static Vector3 RampPoint()
		{
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			int num = Mathf.RoundToInt(_aimPoint.x);
			int num2 = Mathf.RoundToInt(_aimPoint.z);
			float num3 = _lockedHeight ?? (_aimOnTerrain ? TerrainHeights.Get(num, num2, _aimPoint.y) : _aimPoint.y);
			return new Vector3((float)num, num3, (float)num2);
		}

		private static RampShape CurrentRamp()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			return new RampShape(_rampStart.Value, RampPoint(), Width, Blend);
		}

		private static void ShowRamp()
		{
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_0068: Unknown result type (might be due to invalid IL or missing references)
			if (!_rampStart.HasValue)
			{
				Vector3 val = RampPoint();
				Overlay.ShowRampStart(_aimPoint, val);
				Readout.Show(Localization.instance.Localize("$betterhoe_readout_ramp_start", new string[2]
				{
					Width.ToString(CultureInfo.InvariantCulture),
					Metres(Blend)
				}) + "\n" + PointHeightText("$betterhoe_readout_ramp_start_height", val.y));
				return;
			}
			RampShape ramp = CurrentRamp();
			Overlay.ShowRamp(_aimPoint, ramp);
			string text = Localization.instance.Localize("$betterhoe_readout_ramp", new string[3]
			{
				Width.ToString(CultureInfo.InvariantCulture),
				Metres(ramp.Length),
				Metres(Blend)
			});
			text = text + "\n" + PointHeightText("$betterhoe_readout_ramp_end_height", ramp.End.y);
			text = text + "\n" + Localization.instance.Localize("$betterhoe_readout_ramp_slope", new string[2]
			{
				((ramp.Rise >= 0f) ? "+" : "") + Metres(ramp.Rise),
				ramp.SlopeDegrees.ToString("0.#", CultureInfo.InvariantCulture)
			});
			if (!ramp.IsValid)
			{
				text = text + "\n<color=#ff8060>" + Localization.instance.Localize("$betterhoe_ramp_too_short") + "</color>";
			}
			else if (RampAtLimit(ramp))
			{
				text = text + "\n<color=#ff8060>" + Localization.instance.Localize("$betterhoe_readout_limit") + "</color>";
			}
			Readout.Show(text);
		}

		private static string PointHeightText(string token, float height)
		{
			string text = Localization.instance.Localize(token, new string[1] { Metres(height) });
			if (!_lockedHeight.HasValue)
			{
				return text;
			}
			return text + " " + Localization.instance.Localize("$betterhoe_readout_locked");
		}

		private static bool RampAtLimit(RampShape ramp)
		{
			if (ramp.Equals(_rampLimitKey) && Time.frameCount - _rampLimitFrame < 15)
			{
				return _rampLimit;
			}
			_rampLimitKey = ramp;
			_rampLimitFrame = Time.frameCount;
			_rampLimit = RampExceedsLimit(ramp);
			return _rampLimit;
		}

		private static bool RampExceedsLimit(RampShape ramp)
		{
			ramp.Vertices(RampVertices);
			foreach (var (x, z, num, num2) in RampVertices)
			{
				if (TerrainHeights.TryGet(x, z, out var height, out var natural) && Mathf.Abs(Mathf.Lerp(height, num, num2) - natural) > 8.01f)
				{
					return true;
				}
			}
			return false;
		}

		private static bool TryPlaceRamp(Player player, Piece piece, out bool placed)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: 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_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: 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_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			placed = false;
			Vector3 val = RampPoint();
			if (!_rampStart.HasValue)
			{
				if (!TerrainHeights.TryGet((int)val.x, (int)val.z, out var _, out var _))
				{
					((Character)player).Message((MessageType)2, "$msg_invalidplacement", 0, (Sprite)null, false);
					return true;
				}
				_rampStart = val;
				((Character)player).Message((MessageType)1, "$betterhoe_ramp_started", 0, (Sprite)null, false);
				return true;
			}
			RampShape rampShape = CurrentRamp();
			if (!rampShape.IsValid)
			{
				((Character)player).Message((MessageType)2, "$betterhoe_ramp_too_short", 0, (Sprite)null, false);
				return true;
			}
			rampShape.Vertices(RampVertices);
			List<(int, int)> list = new List<(int, int)>();
			foreach (var (item, item2, _, _) in RampVertices)
			{
				list.Add((item, item2));
			}
			if (!CheckAccess(player, list))
			{
				return true;
			}
			TerrainEdits terrainEdits = new TerrainEdits();
			foreach (var (num, num2, worldHeight, weight) in RampVertices)
			{
				terrainEdits.SetHeight(num, num2, worldHeight, weight);
			}
			terrainEdits.Paint(rampShape.Corners(0f), HoeTool.Ramp.PaintColor);
			List<TerrainSnapshot> before = new List<TerrainSnapshot>();
			if (!terrainEdits.Send(rampShape.Center, rampShape.Radius, before))
			{
				((Character)player).Message((MessageType)2, "$msg_invalidplacement", 0, (Sprite)null, false);
				return true;
			}
			UndoHistory.Record(HoeTool.Ramp.NameToken, rampShape.Center, rampShape.Radius, list, before);
			PlayEffects(player, piece, rampShape.Center);
			_rampStart = null;
			placed = true;
			return true;
		}

		private static void Hide()
		{
			Overlay.Hide();
			Readout.Hide();
		}

		private static bool InputBlocked()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Invalid comparison between Unknown and I4
			if (!Menu.IsVisible() && !InventoryGui.IsVisible() && !Hud.IsPieceSelectionVisible() && (!((Object)(object)Chat.instance != (Object)null) || !Chat.instance.HasFocus()) && !Console.IsVisible() && !TextInput.IsVisible())
			{
				if ((Object)(object)Minimap.instance != (Object)null)
				{
					return (int)Minimap.instance.m_mode == 2;
				}
				return false;
			}
			return true;
		}

		private static void HandleInput(Player player, HoeTool tool)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			float y = Input.mouseScrollDelta.y;
			ShapeMode shape;
			if (y != 0f)
			{
				int num = ((y > 0f) ? 1 : (-1));
				if (Keys.Ctrl)
				{
					_width.Value = Mathf.Clamp(Width + num, 1, MaxSize);
				}
				else if (Keys.Shift)
				{
					_length.Value = Mathf.Clamp(Length + num, 1, MaxSize);
				}
				else
				{
					if (Keys.Alt)
					{
						shape = tool.Shape;
						if (shape == ShapeMode.Raise || shape == ShapeMode.Lower)
						{
							int num2 = Mathf.Clamp(Array.IndexOf(Steps, _step.Value) + num, 0, Steps.Length - 1);
							_step.Value = Steps[num2];
							goto IL_010b;
						}
					}
					if (Keys.Alt && tool.Shape == ShapeMode.Ramp)
					{
						int num3 = Mathf.Clamp(Array.IndexOf(Blends, _blend.Value) + num, 0, Blends.Length - 1);
						_blend.Value = Blends[num3];
					}
				}
			}
			goto IL_010b;
			IL_010b:
			if (_rampStart.HasValue && Input.GetMouseButtonDown(1) && !Keys.AnyModifier)
			{
				_rampStart = null;
				BlocksMenu = true;
				((Character)player).Message((MessageType)1, "$betterhoe_ramp_cancelled", 0, (Sprite)null, false);
			}
			shape = tool.Shape;
			if ((shape == ShapeMode.Flatten || shape == ShapeMode.Ramp) && Input.GetMouseButtonDown(2) && !Keys.AnyModifier && HasAim)
			{
				if (_lockedHeight.HasValue)
				{
					_lockedHeight = null;
					((Character)player).Message((MessageType)1, "$betterhoe_height_unlocked", 0, (Sprite)null, false);
				}
				else
				{
					_lockedHeight = _aimPoint.y;
					((Character)player).Message((MessageType)1, Localization.instance.Localize("$betterhoe_height_locked", new string[1] { Metres(_aimPoint.y) }), 0, (Sprite)null, false);
				}
			}
		}

		private static Area CurrentArea()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			return Area.Around(_aimPoint, Width, Length);
		}

		private static float ToolValue(HoeTool tool)
		{
			return tool.Shape switch
			{
				ShapeMode.Flatten => _lockedHeight ?? _aimPoint.y, 
				ShapeMode.Raise => Step, 
				ShapeMode.Lower => 0f - Step, 
				_ => 0f, 
			};
		}

		internal static bool TryPlace(Player player, Piece piece, out bool placed)
		{
			//IL_0093: 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_00db: Unknown result type (might be due to invalid IL or missing references)
			placed = false;
			HoeTool hoeTool = ActiveTool(player);
			if (hoeTool == null || (Object)(object)SelectedHoePiece(player) != (Object)(object)piece)
			{
				return false;
			}
			if (!HasAim)
			{
				((Character)player).Message((MessageType)2, "$msg_invalidplacement", 0, (Sprite)null, false);
				return true;
			}
			if (hoeTool == HoeTool.Ramp)
			{
				return TryPlaceRamp(player, piece, out placed);
			}
			Area area = CurrentArea();
			List<(int, int)> vertices = AreaVertices(area);
			if (!CheckAccess(player, vertices))
			{
				return true;
			}
			float value = ToolValue(hoeTool);
			TerrainEdits terrainEdits = BuildEdits(hoeTool, area, value);
			Vector3 center = default(Vector3);
			((Vector3)(ref center))..ctor(area.CenterX, _aimPoint.y, area.CenterZ);
			List<TerrainSnapshot> before = new List<TerrainSnapshot>();
			if (!terrainEdits.Send(center, area.Radius + 1f, before))
			{
				((Character)player).Message((MessageType)2, "$msg_invalidplacement", 0, (Sprite)null, false);
				return true;
			}
			UndoHistory.Record(hoeTool.NameToken, center, area.Radius + 1f, vertices, before);
			PlayEffects(player, piece, center);
			placed = true;
			return true;
		}

		private static TerrainEdits BuildEdits(HoeTool tool, Area area, float value)
		{
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			TerrainEdits terrainEdits = new TerrainEdits();
			if (tool.Shape != ShapeMode.None)
			{
				for (int i = area.MinX; i <= area.MaxX; i++)
				{
					for (int j = area.MinZ; j <= area.MaxZ; j++)
					{
						switch (tool.Shape)
						{
						case ShapeMode.Flatten:
							terrainEdits.SetHeight(i, j, value);
							break;
						case ShapeMode.Reset:
							terrainEdits.ResetHeight(i, j);
							break;
						default:
							terrainEdits.AddHeight(i, j, value);
							break;
						}
					}
				}
			}
			if (!tool.Paint.HasValue)
			{
				return terrainEdits;
			}
			terrainEdits.Paint(area.MinX, area.MinZ, area.MaxX, area.MaxZ, tool.PaintColor);
			return terrainEdits;
		}

		private static List<(int x, int z)> AreaVertices(Area area)
		{
			List<(int, int)> list = new List<(int, int)>();
			for (int i = area.MinX; i <= area.MaxX; i++)
			{
				for (int j = area.MinZ; j <= area.MaxZ; j++)
				{
					list.Add((i, j));
				}
			}
			return list;
		}

		private static bool CheckAccess(Player player, List<(int x, int z)> vertices)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			bool flag = false;
			Vector3 val = default(Vector3);
			foreach (var (num, num2) in vertices)
			{
				if (TerrainHeights.TryGet(num, num2, out var height, out var _))
				{
					flag = true;
					((Vector3)(ref val))..ctor((float)num, height, (float)num2);
					if (Location.IsInsideNoBuildLocation(val))
					{
						((Character)player).Message((MessageType)2, "$msg_nobuildzone", 0, (Sprite)null, false);
						return false;
					}
					if (!PrivateArea.CheckAccess(val, 0f, false, false))
					{
						PrivateArea.CheckAccess(val, 0f, true, false);
						((Character)player).Message((MessageType)2, "$msg_privatezone", 0, (Sprite)null, false);
						return false;
					}
				}
			}
			if (!flag)
			{
				((Character)player).Message((MessageType)2, "$msg_invalidplacement", 0, (Sprite)null, false);
			}
			return flag;
		}

		private static void PlayEffects(Player player, Piece piece, Vector3 center)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: 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)
			player.FaceLookDirection();
			ItemData rightItem = ((Humanoid)player).GetRightItem();
			if (rightItem != null)
			{
				((Character)player).m_zanim.SetTrigger(rightItem.m_shared.m_attack.m_attackAnimation);
			}
			piece.m_placeEffect.Create(center, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
			TerrainOp component = ((Component)piece).GetComponent<TerrainOp>();
			if (component != null)
			{
				component.m_onPlacedEffect.Create(center, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
			}
		}

		private static string ReadoutText(HoeTool tool, Area area, float value)
		{
			Localization instance = Localization.instance;
			string[] obj = new string[3]
			{
				Localization.instance.Localize(tool.NameToken),
				null,
				null
			};
			int sizeX = area.SizeX;
			obj[1] = sizeX.ToString(CultureInfo.InvariantCulture);
			sizeX = area.SizeZ;
			obj[2] = sizeX.ToString(CultureInfo.InvariantCulture);
			string text = instance.Localize("$betterhoe_readout_area", obj);
			switch (tool.Shape)
			{
			case ShapeMode.Flatten:
				text = text + "\n" + Localization.instance.Localize(_lockedHeight.HasValue ? "$betterhoe_readout_height_locked" : "$betterhoe_readout_height", new string[1] { Metres(value) });
				break;
			case ShapeMode.Raise:
			case ShapeMode.Lower:
				text = text + "\n" + Localization.instance.Localize("$betterhoe_readout_step", new string[1] { ((value >= 0f) ? "+" : "") + Metres(value) });
				break;
			}
			ShapeMode shape = tool.Shape;
			if ((shape == ShapeMode.Flatten || shape == ShapeMode.Raise || shape == ShapeMode.Lower) && CountAtLimit(tool, area, value) > 0)
			{
				text = text + "\n<color=#ff8060>" + Localization.instance.Localize("$betterhoe_readout_limit") + "</color>";
			}
			return text;
		}

		private static int CountAtLimit(HoeTool tool, Area area, float value)
		{
			(Area, HoeTool, float) limitKey = (area, tool, value);
			if (limitKey.Equals(_limitKey) && Time.frameCount - _limitFrame < 15)
			{
				return _limitCount;
			}
			int num = 0;
			for (int i = area.MinX; i <= area.MaxX; i++)
			{
				for (int j = area.MinZ; j <= area.MaxZ; j++)
				{
					if (TerrainHeights.TryGet(i, j, out var height, out var natural) && Mathf.Abs(((tool.Shape == ShapeMode.Flatten) ? value : (height + value)) - natural) > 8.01f)
					{
						num++;
					}
				}
			}
			_limitKey = limitKey;
			_limitCount = num;
			_limitFrame = Time.frameCount;
			return num;
		}

		internal static string Metres(float value)
		{
			return value.ToString("0.##", CultureInfo.InvariantCulture);
		}
	}
	internal enum ShapeMode
	{
		None,
		Flatten,
		Raise,
		Lower,
		Reset,
		Ramp
	}
	internal sealed class HoeTool
	{
		public static readonly HoeTool Flatten = new HoeTool(ShapeMode.Flatten, (PaintType)0, "$betterhoe_tool_flatten", "$betterhoe_hint_flatten");

		public static readonly HoeTool Raise = new HoeTool(ShapeMode.Raise, (PaintType)0, "$betterhoe_tool_raise", "$betterhoe_hint_raise");

		public static readonly HoeTool Lower = new HoeTool(ShapeMode.Lower, (PaintType)0, "$betterhoe_tool_lower", "$betterhoe_hint_lower");

		public static readonly HoeTool Ramp = new HoeTool(ShapeMode.Ramp, (PaintType)0, "$betterhoe_tool_ramp", "$betterhoe_hint_ramp_end");

		public static readonly HoeTool ResetHeight = new HoeTool(ShapeMode.Reset, null, "$betterhoe_tool_reset_height", "$betterhoe_hint_reset_height");

		public static readonly HoeTool PaintDirt = new HoeTool(ShapeMode.None, (PaintType)0, "$betterhoe_tool_path", "$betterhoe_hint_path");

		public static readonly HoeTool PaintPaved = new HoeTool(ShapeMode.None, (PaintType)2, "$betterhoe_tool_paved", "$betterhoe_hint_paved");

		public static readonly HoeTool PaintCultivate = new HoeTool(ShapeMode.None, (PaintType)1, "$betterhoe_tool_cultivate", "$betterhoe_hint_cultivate");

		public static readonly HoeTool PaintReset = new HoeTool(ShapeMode.None, (PaintType)3, "$betterhoe_tool_reset_paint", "$betterhoe_hint_reset_paint");

		public ShapeMode Shape { get; }

		public PaintType? Paint { get; }

		public string NameToken { get; }

		public string ActionToken { get; }

		public Color PaintColor => (Color)(Paint switch
		{
			(PaintType)1L => Heightmap.m_paintMaskPaved, 
			(PaintType)2L => Heightmap.m_paintMaskNothing, 
			(PaintType)0L => Heightmap.m_paintMaskCultivated, 
			_ => Heightmap.m_paintMaskDirt, 
		});

		private HoeTool(ShapeMode shape, PaintType? paint, string nameToken, string actionToken)
		{
			Shape = shape;
			Paint = paint;
			NameToken = nameToken;
			ActionToken = actionToken;
		}
	}
	internal readonly struct RampShape : IEquatable<RampShape>
	{
		public readonly Vector3 Start;

		public readonly Vector3 End;

		public readonly float Width;

		public readonly float Blend;

		public float Length
		{
			get
			{
				//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)
				Vector2 val = new Vector2(End.x - Start.x, End.z - Start.z);
				return ((Vector2)(ref val)).magnitude;
			}
		}

		public float Rise => End.y - Start.y;

		public float SlopeDegrees
		{
			get
			{
				if (!(Length > 0f))
				{
					return 0f;
				}
				return Mathf.Atan2(Rise, Length) * 57.29578f;
			}
		}

		public bool IsValid => Length >= 1f;

		public Vector3 Center => (Start + End) / 2f;

		public float Radius => Length / 2f + Width / 2f + Blend + 1f;

		public Vector3 Along
		{
			get
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				//IL_0020: Unknown result type (might be due to invalid IL or missing references)
				Vector3 val = End - Start;
				val.y = 0f;
				return ((Vector3)(ref val)).normalized;
			}
		}

		public Vector3 Across => new Vector3(0f - Along.z, 0f, Along.x);

		public RampShape(Vector3 start, Vector3 end, float width, float blend)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			Start = start;
			End = end;
			Width = width;
			Blend = blend;
		}

		public float HeightAt(float t)
		{
			return Mathf.Lerp(Start.y, End.y, t);
		}

		public void Project(float x, float z, out float t, out float distance)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = default(Vector3);
			((Vector3)(ref val))..ctor(x - Start.x, 0f, z - Start.z);
			float length = Length;
			t = ((length > 0f) ? (Vector3.Dot(val, Along) / length) : 0f);
			distance = Mathf.Abs(Vector3.Dot(val, Across));
		}

		public float WeightAt(float x, float z, out float height)
		{
			Project(x, z, out var t, out var distance);
			height = HeightAt(Mathf.Clamp01(t));
			if (Mathf.Abs(t - 0.5f) > 0.501f)
			{
				return 0f;
			}
			float num = Width / 2f;
			if (distance <= num + 0.001f)
			{
				return 1f;
			}
			if (Blend <= 0f || distance >= num + Blend)
			{
				return 0f;
			}
			float num2 = 1f - (distance - num) / Blend;
			return num2 * num2 * (3f - 2f * num2);
		}

		public void Vertices(List<(int x, int z, float height, float weight)> result)
		{
			result.Clear();
			float num = Width / 2f + Blend + 1f;
			int num2 = Mathf.FloorToInt(Mathf.Min(Start.x, End.x) - num);
			int num3 = Mathf.CeilToInt(Mathf.Max(Start.x, End.x) + num);
			int num4 = Mathf.FloorToInt(Mathf.Min(Start.z, End.z) - num);
			int num5 = Mathf.CeilToInt(Mathf.Max(Start.z, End.z) + num);
			for (int i = num2; i <= num3; i++)
			{
				for (int j = num4; j <= num5; j++)
				{
					float height;
					float num6 = WeightAt(i, j, out height);
					if (num6 > 0.001f)
					{
						result.Add((i, j, height, num6));
					}
				}
			}
		}

		public Vector3[] Corners(float extra)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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_0067: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = Across * (Width / 2f + extra);
			return (Vector3[])(object)new Vector3[4]
			{
				Start + val,
				Start - val,
				End - val,
				End + val
			};
		}

		public bool Equals(RampShape other)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: 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)
			if (Start == other.Start && End == other.End && Width == other.Width)
			{
				return Blend == other.Blend;
			}
			return false;
		}

		public override bool Equals(object obj)
		{
			if (obj is RampShape other)
			{
				return Equals(other);
			}
			return false;
		}

		public override int GetHashCode()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			int num = (((object)Start/*cast due to .constrained prefix*/).GetHashCode() * 397) ^ ((object)End/*cast due to .constrained prefix*/).GetHashCode();
			float width = Width;
			int num2 = num ^ width.GetHashCode();
			width = Blend;
			return num2 ^ width.GetHashCode();
		}
	}
	internal enum HeightMode : byte
	{
		Set,
		Add,
		Reset
	}
	internal sealed class TerrainEdits
	{
		private readonly struct HeightEdit
		{
			public readonly Vector3 Point;

			public readonly HeightMode Mode;

			public readonly float Value;

			public readonly float Weight;

			public HeightEdit(Vector3 point, HeightMode mode, float value, float weight)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				Point = point;
				Mode = mode;
				Value = value;
				Weight = weight;
			}
		}

		private readonly struct PaintEdit
		{
			public readonly Vector2[] Corners;

			public readonly Color Color;

			public Rect Bounds
			{
				get
				{
					//IL_0007: Unknown result type (might be due to invalid IL or missing references)
					//IL_000c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0014: Unknown result type (might be due to invalid IL or missing references)
					//IL_0019: Unknown result type (might be due to invalid IL or missing references)
					//IL_0027: Unknown result type (might be due to invalid IL or missing references)
					//IL_002c: Unknown result type (might be due to invalid IL or missing references)
					//IL_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_0031: Unknown result type (might be due to invalid IL or missing references)
					//IL_0036: Unknown result type (might be due to invalid IL or missing references)
					//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_003a: Unknown result type (might be due to invalid IL or missing references)
					//IL_003f: Unknown result type (might be due to invalid IL or missing references)
					//IL_004a: 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_0056: Unknown result type (might be due to invalid IL or missing references)
					//IL_005c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0062: Unknown result type (might be due to invalid IL or missing references)
					Vector2 val = Corners[0];
					Vector2 val2 = Corners[0];
					Vector2[] corners = Corners;
					foreach (Vector2 val3 in corners)
					{
						val = Vector2.Min(val, val3);
						val2 = Vector2.Max(val2, val3);
					}
					return Rect.MinMaxRect(val.x, val.y, val2.x, val2.y);
				}
			}

			public PaintEdit(Vector2[] corners, Color color)
			{
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_0009: Unknown result type (might be due to invalid IL or missing references)
				Corners = corners;
				Color = color;
			}

			public bool Contains(Vector2 point)
			{
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0014: Unknown result type (might be due to invalid IL or missing references)
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_002c: Unknown result type (might be due to invalid IL or missing references)
				//IL_002e: 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_003c: 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_004a: 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_0058: Unknown result type (might be due to invalid IL or missing references)
				//IL_005e: Unknown result type (might be due to invalid IL or missing references)
				bool flag = false;
				bool flag2 = false;
				for (int i = 0; i < Corners.Length; i++)
				{
					Vector2 val = Corners[i];
					Vector2 val2 = Corners[(i + 1) % Corners.Length];
					float num = (val2.x - val.x) * (point.y - val.y) - (val2.y - val.y) * (point.x - val.x);
					flag = flag || num < -0.0001f;
					flag2 = flag2 || num > 0.0001f;
				}
				return !(flag && flag2);
			}
		}

		private readonly List<HeightEdit> _heights = new List<HeightEdit>();

		private readonly List<PaintEdit> _paints = new List<PaintEdit>();

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

		public bool IsEmpty
		{
			get
			{
				if (_heights.Count == 0)
				{
					return _paints.Count == 0;
				}
				return false;
			}
		}

		public void SetHeight(float x, float z, float worldHeight, float weight = 1f)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			_heights.Add(new HeightEdit(new Vector3(x, 0f, z), HeightMode.Set, worldHeight, weight));
		}

		public void AddHeight(float x, float z, float delta)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			_heights.Add(new HeightEdit(new Vector3(x, 0f, z), HeightMode.Add, delta, 1f));
		}

		public void ResetHeight(float x, float z)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			_heights.Add(new HeightEdit(new Vector3(x, 0f, z), HeightMode.Reset, 0f, 1f));
		}

		public void Paint(float minX, float minZ, float maxX, float maxZ, Color color)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			_paints.Add(new PaintEdit((Vector2[])(object)new Vector2[4]
			{
				new Vector2(minX, minZ),
				new Vector2(maxX, minZ),
				new Vector2(maxX, maxZ),
				new Vector2(minX, maxZ)
			}, color));
		}

		public void Paint(Vector3[] corners, Color color)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			Vector2[] array = (Vector2[])(object)new Vector2[corners.Length];
			for (int i = 0; i < corners.Length; i++)
			{
				array[i] = new Vector2(corners[i].x, corners[i].z);
			}
			_paints.Add(new PaintEdit(array, color));
		}

		public bool Send(Vector3 center, float radius, List<TerrainSnapshot> before)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			TempMaps.Clear();
			Heightmap.FindHeightmap(center, radius, TempMaps);
			before.Clear();
			List<(TerrainComp, ZPackage)> list = new List<(TerrainComp, ZPackage)>();
			foreach (Heightmap tempMap in TempMaps)
			{
				List<int> heightIndices;
				List<int> paintIndices;
				ZPackage val = Pack(tempMap, center, radius, out heightIndices, out paintIndices);
				if (val != null)
				{
					TerrainComp andCreateTerrainCompiler = tempMap.GetAndCreateTerrainCompiler();
					if (!((Object)(object)andCreateTerrainCompiler == (Object)null) && andCreateTerrainCompiler.m_initialized)
					{
						before.Add(TerrainSnapshot.Capture(andCreateTerrainCompiler, heightIndices, paintIndices));
						list.Add((andCreateTerrainCompiler, val));
					}
				}
			}
			bool flag = false;
			foreach (var item3 in list)
			{
				TerrainComp item = item3.Item1;
				ZPackage item2 = item3.Item2;
				flag |= TerrainRpc.Send(item, item2);
			}
			return flag;
		}

		private ZPackage Pack(Heightmap map, Vector3 center, float radius, out List<int> heightIndices, out List<int> paintIndices)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: 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_0124: 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_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f8: Expected O, but got Unknown
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02be: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
			int width = map.m_width;
			int num = width + 1;
			heightIndices = new List<int>();
			List<HeightEdit> list = new List<HeightEdit>();
			int num2 = default(int);
			int num3 = default(int);
			foreach (HeightEdit height in _heights)
			{
				map.WorldToVertex(height.Point, ref num2, ref num3);
				if (num2 >= 0 && num3 >= 0 && num2 <= width && num3 <= width)
				{
					heightIndices.Add(num3 * num + num2);
					list.Add(height);
				}
			}
			float num4 = (float)width * map.m_scale;
			float num5 = num4 / (float)num;
			Vector3 val = ((Component)map).transform.position - new Vector3(num4 / 2f, 0f, num4 / 2f);
			paintIndices = new List<int>();
			List<Color> list2 = new List<Color>();
			Vector2 point = default(Vector2);
			foreach (PaintEdit paint in _paints)
			{
				Rect bounds = paint.Bounds;
				TexelRange(((Rect)(ref bounds)).xMin - val.x, ((Rect)(ref bounds)).xMax - val.x, num5, width, out var first, out var last);
				TexelRange(((Rect)(ref bounds)).yMin - val.z, ((Rect)(ref bounds)).yMax - val.z, num5, width, out var first2, out var last2);
				for (int i = first2; i <= last2; i++)
				{
					for (int j = first; j <= last; j++)
					{
						((Vector2)(ref point))..ctor(val.x + ((float)j + 0.5f) * num5, val.z + ((float)i + 0.5f) * num5);
						if (paint.Contains(point))
						{
							paintIndices.Add(i * num + j);
							list2.Add(paint.Color);
						}
					}
				}
			}
			if (list.Count == 0 && list2.Count == 0)
			{
				return null;
			}
			ZPackage val2 = new ZPackage();
			val2.Write(1);
			val2.Write(center);
			val2.Write(radius);
			val2.Write(list.Count);
			for (int k = 0; k < list.Count; k++)
			{
				val2.Write(heightIndices[k]);
				val2.Write((byte)list[k].Mode);
				val2.Write(list[k].Value);
				val2.Write(list[k].Weight);
			}
			val2.Write(list2.Count);
			for (int l = 0; l < list2.Count; l++)
			{
				val2.Write(paintIndices[l]);
				val2.Write(list2[l].r);
				val2.Write(list2[l].g);
				val2.Write(list2[l].b);
			}
			return val2;
		}

		private static void TexelRange(float from, float to, float texel, int width, out int first, out int last)
		{
			first = Mathf.Max(0, Mathf.CeilToInt(from / texel - 0.5f - 0.0001f));
			last = Mathf.Min(width, Mathf.CeilToInt(to / texel - 0.5f - 0.0001f) - 1);
		}
	}
	internal static class TerrainHeights
	{
		public static bool TryGet(int x, int z, out float height, out float natural)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = default(Vector3);
			((Vector3)(ref val))..ctor((float)x, 0f, (float)z);
			Heightmap val2 = Heightmap.FindHeightmap(val);
			if ((Object)(object)val2 == (Object)null)
			{
				height = 0f;
				natural = 0f;
				return false;
			}
			int num = default(int);
			int num2 = default(int);
			val2.WorldToVertex(val, ref num, ref num2);
			height = val2.GetHeight(num, num2) + ((Component)val2).transform.position.y;
			natural = height;
			TerrainComp val3 = TerrainComp.FindTerrainCompiler(((Component)val2).transform.position);
			if ((Object)(object)val3 != (Object)null && val3.m_initialized)
			{
				int num3 = num2 * val3.m_pitch + num;
				if (num3 >= 0 && num3 < val3.m_levelDelta.Length)
				{
					natural -= val3.m_levelDelta[num3] + val3.m_smoothDelta[num3];
				}
			}
			return true;
		}

		public static float GroundAt(float x, float z, float fallback)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			float result = default(float);
			if (!((Object)(object)ZoneSystem.instance != (Object)null) || !ZoneSystem.instance.GetGroundHeight(new Vector3(x, 0f, z), ref result))
			{
				return fallback;
			}
			return result;
		}

		public static float Get(int x, int z, float fallback)
		{
			if (!TryGet(x, z, out var height, out var _))
			{
				return fallback;
			}
			return height;
		}
	}
	internal static class TerrainRpc
	{
		public const int Version = 1;

		private const string EditRpc = "BetterHoe_Edit";

		private const string RestoreRpc = "BetterHoe_Restore";

		public const float MaxLevelDelta = 8f;

		public static void Register(TerrainComp compiler)
		{
			if (!((Object)(object)compiler.m_nview == (Object)null) && compiler.m_initialized)
			{
				compiler.m_nview.Register<ZPackage>("BetterHoe_Edit", (Action<long, ZPackage>)delegate(long _, ZPackage package)
				{
					OnEdit(compiler, package);
				});
				compiler.m_nview.Register<ZPackage>("BetterHoe_Restore", (Action<long, ZPackage>)delegate(long _, ZPackage package)
				{
					OnRestore(compiler, package);
				});
			}
		}

		public static bool SendRestore(TerrainComp compiler, ZPackage package)
		{
			if ((Object)(object)compiler.m_nview == (Object)null || !compiler.m_nview.IsValid())
			{
				return false;
			}
			compiler.m_nview.InvokeRPC("BetterHoe_Restore", new object[1] { package });
			return true;
		}

		private static void OnRestore(TerrainComp compiler, ZPackage package)
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)compiler == (Object)null || !compiler.m_initialized || !compiler.m_nview.IsValid() || !compiler.m_nview.IsOwner())
			{
				return;
			}
			int num = package.ReadInt();
			if (num != 1)
			{
				Logger.LogWarning((object)$"Ignoring a terrain restore with version {num}; this client uses version {1}.");
				return;
			}
			Vector3 center = package.ReadVector3();
			float radius = package.ReadSingle();
			int num2 = package.ReadInt();
			for (int i = 0; i < num2; i++)
			{
				int num3 = package.ReadInt();
				bool flag = package.ReadBool();
				float num4 = package.ReadSingle();
				float num5 = package.ReadSingle();
				if (num3 >= 0 && num3 < compiler.m_modifiedHeight.Length)
				{
					compiler.m_modifiedHeight[num3] = flag;
					compiler.m_levelDelta[num3] = num4;
					compiler.m_smoothDelta[num3] = num5;
				}
			}
			int num6 = package.ReadInt();
			Color val = default(Color);
			for (int j = 0; j < num6; j++)
			{
				int num7 = package.ReadInt();
				bool flag2 = package.ReadBool();
				((Color)(ref val))..ctor(package.ReadSingle(), package.ReadSingle(), package.ReadSingle(), package.ReadSingle());
				if (num7 >= 0 && num7 < compiler.m_paintMask.Length)
				{
					compiler.m_modifiedPaint[num7] = flag2;
					compiler.m_paintMask[num7] = val;
				}
			}
			Finish(compiler, center, radius, num2 == 0);
		}

		public static bool Send(TerrainComp compiler, ZPackage package)
		{
			if ((Object)(object)compiler.m_nview == (Object)null || !compiler.m_nview.IsValid())
			{
				return false;
			}
			compiler.m_nview.InvokeRPC("BetterHoe_Edit", new object[1] { package });
			return true;
		}

		private static void OnEdit(TerrainComp compiler, ZPackage package)
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: 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)
			if ((Object)(object)compiler == (Object)null || !compiler.m_initialized || !compiler.m_nview.IsValid() || !compiler.m_nview.IsOwner())
			{
				return;
			}
			int num = package.ReadInt();
			if (num != 1)
			{
				Logger.LogWarning((object)$"Ignoring a terrain edit with version {num}; this client uses version {1}.");
				return;
			}
			Vector3 center = package.ReadVector3();
			float radius = package.ReadSingle();
			float y = ((Component)compiler).transform.position.y;
			int pitch = compiler.m_pitch;
			Heightmap hmap = compiler.m_hmap;
			int num2 = package.ReadInt();
			for (int i = 0; i < num2; i++)
			{
				int num3 = package.ReadInt();
				HeightMode heightMode = (HeightMode)package.ReadByte();
				float num4 = package.ReadSingle();
				float num5 = package.ReadSingle();
				if (num3 >= 0 && num3 < compiler.m_modifiedHeight.Length)
				{
					if (heightMode == HeightMode.Reset)
					{
						compiler.m_levelDelta[num3] = 0f;
						compiler.m_smoothDelta[num3] = 0f;
						compiler.m_modifiedHeight[num3] = false;
					}
					else
					{
						float height = hmap.GetHeight(num3 % pitch, num3 / pitch);
						float num6 = ((heightMode == HeightMode.Add) ? (height + num4) : Mathf.Lerp(height, num4 - y, Mathf.Clamp01(num5))) - height + compiler.m_smoothDelta[num3];
						compiler.m_smoothDelta[num3] = 0f;
						compiler.m_levelDelta[num3] = Mathf.Clamp(compiler.m_levelDelta[num3] + num6, -8f, 8f);
						compiler.m_modifiedHeight[num3] = true;
					}
				}
			}
			int num7 = package.ReadInt();
			for (int j = 0; j < num7; j++)
			{
				int num8 = package.ReadInt();
				float r = package.ReadSingle();
				float g = package.ReadSingle();
				float b = package.ReadSingle();
				if (num8 >= 0 && num8 < compiler.m_paintMask.Length)
				{
					Color val = compiler.m_paintMask[num8];
					val.r = r;
					val.g = g;
					val.b = b;
					compiler.m_paintMask[num8] = val;
					compiler.m_modifiedPaint[num8] = true;
				}
			}
			Finish(compiler, center, radius, num2 == 0);
		}

		private static void Finish(TerrainComp compiler, Vector3 center, float radius, bool paintOnly)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			compiler.m_operations++;
			compiler.m_lastOpPoint = center;
			compiler.m_lastOpRadius = radius;
			compiler.Save(false);
			compiler.m_hmap.Poke(1, paintOnly);
			if ((Object)(object)ClutterSystem.instance != (Object)null)
			{
				ClutterSystem.instance.ResetGrass(center, radius);
			}
		}
	}
}
namespace BetterHoe.Pieces
{
	internal static class HoePieces
	{
		public const string LowerName = "BetterHoe_Lower";

		public const string RampName = "BetterHoe_Ramp";

		public const string ResetHeightName = "BetterHoe_ResetHeight";

		public const string ResetPaintName = "BetterHoe_ResetPaint";

		public const string CultivateName = "BetterHoe_Cultivate";

		private const string HoeTable = "_HoePieceTable";

		private const string LevelPrefab = "mud_road_v2";

		private const string RaisePrefab = "raise_v2";

		private const string PathPrefab = "path_v2";

		private const string PavedPrefab = "paved_road_v2";

		private const string ReplantPrefab = "replant_v2";

		private const string CultivatePrefab = "cultivate_v2";

		public static void Register()
		{
			PrefabManager.OnVanillaPrefabsAvailable += AddPieces;
			PieceManager.OnPiecesRegistered += OrderPieces;
		}

		private static void AddPieces()
		{
			PrefabManager.OnVanillaPrefabsAvailable -= AddPieces;
			string[] array = new string[4] { "mud_road_v2", "raise_v2", "path_v2", "paved_road_v2" };
			foreach (string text in array)
			{
				GameObject prefab = PrefabManager.Instance.GetPrefab(text);
				Piece val = ((prefab != null) ? prefab.GetComponent<Piece>() : null);
				if (!((Object)(object)val == (Object)null))
				{
					val.m_craftingStation = null;
					if (text == "mud_road_v2" || text == "raise_v2")
					{
						val.m_resources = Array.Empty<Requirement>();
					}
				}
			}
			GameObject prefab2 = PrefabManager.Instance.GetPrefab("raise_v2");
			Sprite val2 = ((prefab2 == null) ? null : prefab2.GetComponent<Piece>()?.m_icon);
			AddPiece("BetterHoe_Ramp", "mud_road_v2", "$betterhoe_piece_ramp", "$betterhoe_piece_ramp_description", ((Object)(object)val2 != (Object)null) ? IconMaker.Ramp(val2) : null);
			AddPiece("BetterHoe_Lower", "raise_v2", "$betterhoe_piece_lower", "$betterhoe_piece_lower_description", ((Object)(object)val2 != (Object)null) ? IconMaker.FlipVertically(val2) : null);
			AddPiece("BetterHoe_ResetHeight", "mud_road_v2", "$betterhoe_piece_reset_height", "$betterhoe_piece_reset_height_description", ((Object)(object)val2 != (Object)null) ? IconMaker.ResetArrow(val2) : null);
			AddPiece("BetterHoe_Cultivate", "cultivate_v2", "$betterhoe_piece_cultivate", "$betterhoe_piece_cultivate_description", null);
			AddPiece("BetterHoe_ResetPaint", "replant_v2", "$betterhoe_piece_reset_paint", "$betterhoe_piece_reset_paint_description", null);
		}

		private static void AddPiece(string name, string baseName, string displayName, string description, Sprite icon)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: 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_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			CustomPiece val = new CustomPiece(name, baseName, new PieceConfig
			{
				Name = displayName,
				Description = description,
				PieceTable = "_HoePieceTable",
				Icon = icon
			});
			val.Piece.m_resources = Array.Empty<Requirement>();
			val.Piece.m_craftingStation = null;
			val.Piece.m_groundPiece = true;
			val.Piece.m_cultivatedGroundOnly = false;
			PieceManager.Instance.AddPiece(val);
		}

		private static void OrderPieces()
		{
			PieceTable pieceTable = PieceManager.Instance.GetPieceTable("_HoePieceTable");
			if (!((Object)(object)pieceTable == (Object)null))
			{
				MoveAfter(pieceTable.m_pieces, "BetterHoe_Ramp", "mud_road_v2");
				MoveAfter(pieceTable.m_pieces, "BetterHoe_Lower", "raise_v2");
				MoveAfter(pieceTable.m_pieces, "BetterHoe_ResetHeight", "BetterHoe_Lower");
				MoveAfter(pieceTable.m_pieces, "BetterHoe_Cultivate", "paved_road_v2");
				MoveAfter(pieceTable.m_pieces, "BetterHoe_ResetPaint", "BetterHoe_Cultivate");
			}
		}

		private static void MoveAfter(List<GameObject> pieces, string name, string anchorName)
		{
			int num = pieces.FindIndex((GameObject p) => (Object)(object)p != (Object)null && ((Object)p).name == name);
			if (num >= 0)
			{
				GameObject item = pieces[num];
				pieces.RemoveAt(num);
				int num2 = pieces.FindIndex((GameObject p) => (Object)(object)p != (Object)null && ((Object)p).name == anchorName);
				pieces.Insert((num2 < 0) ? pieces.Count : (num2 + 1), item);
			}
		}
	}
	internal static class IconMaker
	{
		public static Sprite FlipVertically(Sprite source)
		{
			return ToSprite(Copy(source, flip: true), source, "_flipped");
		}

		public static Sprite ResetArrow(Sprite source)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: 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_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: 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_0169: 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_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: 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_0184: 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_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: 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_0254: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: 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_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = Copy(source, flip: false);
			int width = ((Texture)val).width;
			int height = ((Texture)val).height;
			Color[] pixels = val.GetPixels();
			int num = Mathf.RoundToInt((float)height * 0.45f);
			Color val2 = Color.clear;
			float num2 = 0f;
			for (int i = num; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					Color val3 = pixels[i * width + j];
					val2 += val3 * val3.a;
					num2 += val3.a;
					pixels[i * width + j] = Color.clear;
				}
			}
			val2 = (Color)((num2 > 0f) ? (val2 / num2) : new Color(0.85f, 0.75f, 0.55f));
			val2.a = 1f;
			Vector2 val4 = default(Vector2);
			((Vector2)(ref val4))..ctor((float)width * 0.5f, (float)height * 0.73f);
			float num3 = (float)width * 0.15f;
			float num4 = (float)width * 0.055f;
			Vector2 val5 = val4 + new Vector2(Mathf.Cos(5.7595863f), Mathf.Sin(5.7595863f)) * num3;
			Vector2 val6 = default(Vector2);
			((Vector2)(ref val6))..ctor(0f - Mathf.Sin(5.7595863f), Mathf.Cos(5.7595863f));
			Vector2 val7 = default(Vector2);
			((Vector2)(ref val7))..ctor(Mathf.Cos(5.7595863f), Mathf.Sin(5.7595863f));
			float num5 = num4 * 1.6f;
			Vector2 a = val5 + val6 * num5 * 1.2f;
			Vector2 b = val5 + val7 * num5;
			Vector2 c = val5 - val7 * num5;
			Vector2 p = default(Vector2);
			for (int k = num; k < height; k++)
			{
				for (int l = 0; l < width; l++)
				{
					int num6 = 0;
					for (int m = 0; m < 4; m++)
					{
						for (int n = 0; n < 4; n++)
						{
							((Vector2)(ref p))..ctor((float)l + ((float)n + 0.5f) / 4f, (float)k + ((float)m + 0.5f) / 4f);
							if (InArc(p, val4, num3, num4, 60f, 330f) || InTriangle(p, a, b, c))
							{
								num6++;
							}
						}
					}
					if (num6 > 0)
					{
						Color val8 = val2;
						val8.a = (float)num6 / 16f;
						pixels[k * width + l] = val8;
					}
				}
			}
			val.SetPixels(pixels);
			val.Apply();
			return ToSprite(val, source, "_reset");
		}

		public static Sprite Ramp(Sprite source)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: 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_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: 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_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: 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_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//