Decompiled source of PotatoPainter v1.0.1

plugins/PotatoPainter.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.PostProcessing;
using UnityEngine.Rendering;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace PotatoPainter;

internal static class Bend
{
	internal sealed class Bender
	{
		internal Mesh Mesh;

		internal Renderer Host;

		internal Renderer Paint;

		internal Material HostMat;

		internal Vector3[] HostLocal;

		internal Vector2[] HostUV2;

		internal int[] Corners;

		internal Vector3[] Bary;

		internal bool[] Ok;

		internal float LiftLocal;

		internal Vector3[] Buf;

		internal Vector3[] Nrm;

		internal Vector3[] Bent;

		internal int[] BentFrame;
	}

	internal static readonly List<Bender> All = new List<Bender>();

	private static readonly int Wind1 = Shader.PropertyToID("_GlobalWind1");

	private static readonly int Wind2 = Shader.PropertyToID("_GlobalWind2");

	private static readonly int WindAlpha = Shader.PropertyToID("_GlobalWindAlpha");

	internal static bool Sways(Material m)
	{
		if (Object.op_Implicit((Object)(object)m) && Object.op_Implicit((Object)(object)m.shader) && ((Object)m.shader).name == "Custom/Vegetation")
		{
			if (m.GetFloat("_SwayDistance") == 0f && m.GetFloat("_RippleDistance") == 0f)
			{
				return m.GetFloat("_PushDistance") != 0f;
			}
			return true;
		}
		return false;
	}

	private static Vector3 Bent(Vector3 v, float uv2y, Material m, Matrix4x4 l2w, Matrix4x4 w2l, float t, Vector4 w1, Vector4 w2, float alpha)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: 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_00c9: 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_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: 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_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0110: Unknown result type (might be due to invalid IL or missing references)
		//IL_011a: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		float num = m.GetFloat("_Height");
		float speed = m.GetFloat("_SwaySpeed");
		float dist = m.GetFloat("_SwayDistance");
		bool flag = m.HasProperty("_UV2Height") && m.GetFloat("_UV2Height") != 0f;
		float num2 = Mathf.Clamp01(flag ? uv2y : ((num != 0f) ? (v.y / num) : 0f));
		float h2 = num2 * num2;
		float objX = l2w.m03;
		float objZ = l2w.m23;
		Vector3 val = Block(w1);
		Vector3 val2 = Block(w2);
		Vector3 val3 = val + (val2 - val) * alpha;
		float magnitude = ((Vector3)(ref v)).magnitude;
		Vector3 val4 = v + val3;
		return ((((Vector3)(ref val4)).sqrMagnitude > 1E-12f) ? (((Vector3)(ref val4)).normalized * magnitude) : v) + Flutter(v, num2, flag, m, t, w1, w2, alpha);
		Vector3 Block(Vector4 W)
		{
			//IL_0000: 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_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: 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_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			float num3 = (W.w * 0.5f + 0.5f) * speed;
			float num4 = t * num3 + objX;
			float num5 = num3 * t * 0.61234f + objZ;
			return ((Matrix4x4)(ref w2l)).MultiplyVector(new Vector3(W.x, W.y, W.z)) * (dist * W.w * h2 * (Mathf.Sin(num5) * Mathf.Sin(num4) + 0.5f));
		}
	}

	private static Vector3 Flutter(Vector3 v, float h, bool useUV2, Material m, float t, Vector4 w1, Vector4 w2, float alpha)
	{
		//IL_000b: 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_005d: 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_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		float rd = (m.HasProperty("_RippleDistance") ? m.GetFloat("_RippleDistance") : 0f);
		if (rd == 0f)
		{
			return Vector3.zero;
		}
		float w3 = Weight(v, m);
		if (w3 == 0f)
		{
			return Vector3.zero;
		}
		float rs = m.GetFloat("_RippleSpeed");
		Vector3 val = Block(w1);
		Vector3 val2 = Block(w2);
		return val + (val2 - val) * alpha;
		Vector3 Block(Vector4 W)
		{
			//IL_0006: 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_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			float num = t * (W.w * 0.5f + 0.5f) * rs;
			float num2 = Mathf.Sin(0.61234f * num + 10f * v.x + 10f * v.y);
			float num3 = rd * w3 * W.w * (useUV2 ? h : 1f);
			return new Vector3(num2 * Mathf.Sin(num + 10f * v.x + 10f * v.y) * 0.1f, Mathf.Sin(num + 10f * v.z + 10f * v.x) * 0.05f, num2 * Mathf.Sin(num + 10f * v.z + 10f * v.y) * 0.1f) * num3;
		}
	}

	internal static float Weight(Vector3 v, Material m)
	{
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: 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)
		if (!Object.op_Implicit((Object)(object)m) || !m.HasProperty("_RippleDistance") || m.GetFloat("_RippleDistance") == 0f)
		{
			return 0f;
		}
		float num = m.GetFloat("_RippleDeadzoneMin");
		float num2 = m.GetFloat("_RippleDeadzoneMax");
		float num3 = Mathf.Sqrt(v.x * v.x + v.z * v.z);
		if (!(num2 > num))
		{
			if (!(num3 > num))
			{
				return 0f;
			}
			return 1f;
		}
		return Mathf.Clamp01((num3 - num) / (num2 - num));
	}

	internal static void Update()
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_0167: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0180: 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_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01da: Unknown result type (might be due to invalid IL or missing references)
		//IL_01df: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0202: Unknown result type (might be due to invalid IL or missing references)
		//IL_021b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0220: Unknown result type (might be due to invalid IL or missing references)
		//IL_0225: Unknown result type (might be due to invalid IL or missing references)
		//IL_0232: Unknown result type (might be due to invalid IL or missing references)
		//IL_0237: Unknown result type (might be due to invalid IL or missing references)
		//IL_023c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
		if (All.Count == 0)
		{
			return;
		}
		float t = Time.timeSinceLevelLoad / 20f;
		Vector4 w1 = Shader.GetGlobalVector(Wind1);
		Vector4 w2 = Shader.GetGlobalVector(Wind2);
		float alpha = Shader.GetGlobalFloat(WindAlpha);
		int frame = Time.frameCount;
		All.RemoveAll((Bender bender) => !Object.op_Implicit((Object)(object)bender.Paint) || !Object.op_Implicit((Object)(object)bender.Host));
		foreach (Bender item in All)
		{
			Bender b = item;
			if (!b.Paint.isVisible)
			{
				continue;
			}
			Matrix4x4 l2w = ((Component)b.Host).transform.localToWorldMatrix;
			Matrix4x4 w2l = ((Component)b.Host).transform.worldToLocalMatrix;
			for (int num = 0; num < b.Ok.Length; num++)
			{
				if (b.Ok[num])
				{
					Vector3 val = Corner(b.Corners[3 * num]);
					Vector3 val2 = Corner(b.Corners[3 * num + 1]);
					Vector3 val3 = Corner(b.Corners[3 * num + 2]);
					Vector3 val4 = Vector3.Cross(val2 - val, val3 - val);
					Vector3 val5 = ((Vector3)(ref val4)).normalized;
					if (Vector3.Dot(val5, b.Nrm[num]) < 0f)
					{
						val5 = -val5;
					}
					b.Buf[num] = val * b.Bary[num].x + val2 * b.Bary[num].y + val3 * b.Bary[num].z + val5 * b.LiftLocal;
				}
			}
			b.Mesh.vertices = b.Buf;
			b.Mesh.RecalculateBounds();
			Vector3 Corner(int idx)
			{
				//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
				//IL_0030: Unknown result type (might be due to invalid IL or missing references)
				//IL_006b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0071: Unknown result type (might be due to invalid IL or missing references)
				//IL_007d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0083: Unknown result type (might be due to invalid IL or missing references)
				//IL_008e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0093: Unknown result type (might be due to invalid IL or missing references)
				if (b.BentFrame[idx] != frame)
				{
					b.Bent[idx] = Bent(b.HostLocal[idx], (b.HostUV2 != null) ? b.HostUV2[idx].y : 0f, b.HostMat, l2w, w2l, t, w1, w2, alpha);
					b.BentFrame[idx] = frame;
				}
				return b.Bent[idx];
			}
		}
	}
}
internal static class GlowCap
{
	private static readonly float[] Hue = new float[9] { 0f, 0.0445f, 0.0833f, 0.1667f, 0.3333f, 0.5f, 0.6667f, 0.75f, 0.8333f };

	private static readonly float[] WithBloom = new float[9] { 0.76f, 0.74f, 0.77f, 0.8f, 0.88f, 0.83f, 0.77f, 0.77f, 0.71f };

	private static readonly float[] WithoutBloom = new float[9] { 0.9f, 0.84f, 0.91f, 0.92f, 1.08f, 0.97f, 0.93f, 0.94f, 0.78f };

	private static readonly float[] HalfWithBloom = new float[9] { 0.83f, 0.84f, 0.82f, 0.76f, 0.83f, 0.83f, 1.01f, 0.99f, 0.76f };

	private static readonly float[] HalfWithoutBloom = new float[9] { 0.95f, 1f, 0.94f, 0.87f, 0.99f, 1.01f, 1.22f, 1.3f, 0.84f };

	private const float White = 1f;

	internal const float Margin = 0.85f;

	private static bool bloom;

	private static int frame = -1;

	private static bool? lastBloom;

	internal static bool Bloom
	{
		get
		{
			if (frame == Time.frameCount)
			{
				return bloom;
			}
			frame = Time.frameCount;
			bloom = false;
			try
			{
				Camera mainCamera = Utils.GetMainCamera();
				PostProcessingBehaviour val = (Object.op_Implicit((Object)(object)mainCamera) ? ((Component)mainCamera).GetComponent<PostProcessingBehaviour>() : null);
				bloom = Object.op_Implicit((Object)(object)val) && ((Behaviour)val).enabled && Object.op_Implicit((Object)(object)val.profile) && ((PostProcessingModel)val.profile.bloom).enabled;
			}
			catch
			{
				bloom = false;
			}
			return bloom;
		}
	}

	internal static float Cap(Color glow, bool withBloom)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		float h = default(float);
		float num = default(float);
		float num2 = default(float);
		Color.RGBToHSV(glow, ref h, ref num, ref num2);
		float num3 = AtHue(withBloom ? WithBloom : WithoutBloom, h);
		float num4 = AtHue(withBloom ? HalfWithBloom : HalfWithoutBloom, h);
		return 0.85f * ((num >= 0.5f) ? Mathf.Lerp(num4, num3, (num - 0.5f) * 2f) : Mathf.Lerp(1f, num4, num * 2f));
	}

	private static float AtHue(float[] caps, float h)
	{
		int num = Hue.Length;
		if (h >= Hue[num - 1])
		{
			return Mathf.Lerp(caps[num - 1], caps[0], (h - Hue[num - 1]) / (1f - Hue[num - 1]));
		}
		int i;
		for (i = 0; i < num - 2 && h >= Hue[i + 1]; i++)
		{
		}
		return Mathf.Lerp(caps[i], caps[i + 1], (h - Hue[i]) / (Hue[i + 1] - Hue[i]));
	}

	internal static Color Emission(Color kept, bool share)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		return Emission(kept, share, Bloom);
	}

	internal static Color Emission(Color kept, bool share, bool withBloom)
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: 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_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		float maxColorComponent = ((Color)(ref kept)).maxColorComponent;
		if (maxColorComponent <= 0f)
		{
			return Color.black;
		}
		Color val = kept / maxColorComponent;
		float num = Cap(val, withBloom);
		float num2 = (share ? (Mathf.Clamp01(maxColorComponent) * num) : Mathf.Min(maxColorComponent, num));
		Color result = val * num2;
		result.a = 1f;
		return result;
	}

	internal static void Update()
	{
		if (!Object.op_Implicit((Object)(object)Player.m_localPlayer))
		{
			return;
		}
		bool flag = Bloom;
		if (lastBloom == flag)
		{
			return;
		}
		bool num = !lastBloom.HasValue;
		lastBloom = flag;
		if (num)
		{
			return;
		}
		foreach (Paint item in Paint.Loaded)
		{
			if (Object.op_Implicit((Object)(object)item) && item.HasData && item.Etched)
			{
				item.Retint();
			}
		}
	}
}
internal static class Grass
{
	private struct Area
	{
		internal Vector3 Center;

		internal Vector3 Side;

		internal Vector3 Up;

		internal Vector3 Normal;

		internal float Half;
	}

	[HarmonyPatch(typeof(ClutterSystem), "GenerateVegPatch")]
	private static class Patch
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_020b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: Expected O, but got Unknown
			//IL_0267: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: Expected O, but got Unknown
			//IL_0275: Unknown result type (might be due to invalid IL or missing references)
			//IL_027b: Expected O, but got Unknown
			//IL_0289: Unknown result type (might be due to invalid IL or missing references)
			//IL_028f: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			MethodInfo info = AccessTools.Method(typeof(ClutterSystem), "GetGroundInfo", (Type[])null, (Type[])null);
			MethodInfo methodInfo = AccessTools.Method(typeof(Grass), "Hidden", (Type[])null, (Type[])null);
			ParameterInfo[] array = info?.GetParameters();
			int num = ((info == null) ? (-1) : list.FindIndex((CodeInstruction c) => CodeInstructionExtensions.Calls(c, info)));
			if (num < 5 || array == null || array.Length != 5 || !(array[1].ParameterType == typeof(Vector3).MakeByRefType()) || (!(list[num - 4].opcode == OpCodes.Ldloca_S) && !(list[num - 4].opcode == OpCodes.Ldloca)) || num + 3 >= list.Count || (!(list[num + 1].opcode == OpCodes.Brfalse) && !(list[num + 1].opcode == OpCodes.Brfalse_S)) || !(list[num + 1].operand is Label) || (!(list[num + 2].opcode == OpCodes.Ldloc_S) && !(list[num + 2].opcode == OpCodes.Ldloc)) || !(list[num + 3].opcode == OpCodes.Ldfld) || !(list[num + 3].operand is FieldInfo fieldInfo) || !(fieldInfo.DeclaringType == typeof(Clutter)))
			{
				Plugin.Log.LogWarning((object)"Grass stays under ground paint this session: the game's clutter placement isn't laid out as expected (ClutterSystem.GenerateVegPatch).");
				return list;
			}
			Label label = (Label)list[num + 1].operand;
			CodeInstruction val = new CodeInstruction(OpCodes.Ldloc, list[num - 4].operand);
			val.labels.AddRange(list[num + 2].labels);
			list[num + 2].labels.Clear();
			list.InsertRange(num + 2, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[4]
			{
				val,
				new CodeInstruction(OpCodes.Ldloc, list[num + 2].operand),
				new CodeInstruction(OpCodes.Call, (object)methodInfo),
				new CodeInstruction(OpCodes.Brtrue, (object)label)
			});
			Hooked = true;
			return list;
		}
	}

	internal const float Margin = 0.05f;

	internal const float Depth = 0.75f;

	internal const float ReachCap = 0.5f;

	private const float ResetReach = 2.5f;

	private static readonly Dictionary<Clutter, float> reach = new Dictionary<Clutter, float>();

	private static readonly Dictionary<object, Area> areas = new Dictionary<object, Area>();

	private static Area[] list = new Area[0];

	internal static bool Hooked;

	internal static void Set(object key, bool on, Frame f, float size)
	{
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: 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_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		if (areas.TryGetValue(key, out var value))
		{
			if (on)
			{
				Vector3 val = value.Center - f.Center;
				if (((Vector3)(ref val)).sqrMagnitude < 0.0001f && Mathf.Abs(value.Half - size * 0.5f) < 0.001f)
				{
					return;
				}
			}
			areas.Remove(key);
			Reset(value);
		}
		if (on)
		{
			Area area = new Area
			{
				Center = f.Center,
				Normal = f.Normal,
				Up = f.Up,
				Side = Vector3.Cross(f.Normal, f.Up),
				Half = size * 0.5f
			};
			areas[key] = area;
			Reset(area);
		}
		list = areas.Values.ToArray();
	}

	internal static void Clear(object key)
	{
		if (areas.TryGetValue(key, out var value))
		{
			areas.Remove(key);
			Reset(value);
			list = areas.Values.ToArray();
		}
	}

	private static void Reset(Area a)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)ClutterSystem.instance))
		{
			ClutterSystem.instance.ResetGrass(a.Center, a.Half + 2.5f);
		}
	}

	public static bool Hidden(Vector3 p, Clutter clutter)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: 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_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		Area[] array = list;
		if (array.Length == 0)
		{
			return false;
		}
		float num = Reach(clutter);
		for (int i = 0; i < array.Length; i++)
		{
			Vector3 val = p - array[i].Center;
			float num2 = array[i].Half + num;
			if (!(((Vector3)(ref val)).sqrMagnitude > num2 * num2 * 2f + 0.5625f) && Mathf.Abs(Vector3.Dot(val, array[i].Side)) <= num2 && Mathf.Abs(Vector3.Dot(val, array[i].Up)) <= num2 && Mathf.Abs(Vector3.Dot(val, array[i].Normal)) <= 0.75f)
			{
				return true;
			}
		}
		return false;
	}

	private static float Reach(Clutter c)
	{
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_015a: Unknown result type (might be due to invalid IL or missing references)
		//IL_016c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: 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_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		if (c == null)
		{
			return 0f;
		}
		if (reach.TryGetValue(c, out var value))
		{
			return value;
		}
		float num = 0f;
		float num2 = 0f;
		GameObject prefab = c.m_prefab;
		InstanceRenderer val = (Object.op_Implicit((Object)(object)prefab) ? prefab.GetComponent<InstanceRenderer>() : null);
		if (c.m_instanced && Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)val.m_mesh))
		{
			Bounds bounds = val.m_mesh.bounds;
			num = Mathf.Max((Mathf.Abs(((Bounds)(ref bounds)).center.x) + ((Bounds)(ref bounds)).extents.x) * Mathf.Abs(val.m_scale.x), (Mathf.Abs(((Bounds)(ref bounds)).center.z) + ((Bounds)(ref bounds)).extents.z) * Mathf.Abs(val.m_scale.z));
			num2 = (((Bounds)(ref bounds)).center.y + ((Bounds)(ref bounds)).extents.y) * Mathf.Abs(val.m_scale.y);
		}
		else if (Object.op_Implicit((Object)(object)prefab))
		{
			Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Bounds bounds2 = componentsInChildren[i].bounds;
				Vector3 position = prefab.transform.position;
				num = Mathf.Max(new float[5]
				{
					num,
					Mathf.Abs(((Bounds)(ref bounds2)).min.x - position.x),
					Mathf.Abs(((Bounds)(ref bounds2)).max.x - position.x),
					Mathf.Abs(((Bounds)(ref bounds2)).min.z - position.z),
					Mathf.Abs(((Bounds)(ref bounds2)).max.z - position.z)
				});
				num2 = Mathf.Max(num2, ((Bounds)(ref bounds2)).max.y - position.y);
			}
		}
		float num3 = Mathf.Max(new float[3] { c.m_scaleMin, c.m_scaleMax, 0f });
		float num4 = Mathf.Clamp(Mathf.Max(Mathf.Abs(c.m_maxTilt), Mathf.Abs(c.m_minTilt)), 0f, 90f) * ((float)Math.PI / 180f);
		value = Mathf.Min((num + num2 * Mathf.Sin(num4)) * num3 + 0.05f, 0.5f);
		reach[c] = value;
		return value;
	}
}
internal static class Ground
{
	internal sealed class Sheet
	{
		internal Mesh Mesh;

		internal Renderer R;

		internal Heightmap Hm;

		internal Transform Space;

		internal int[] Verts;

		internal int[] Tri;

		internal Vector2[] Domain;

		internal Vector3[] Base;

		internal Vector3[] Normal;

		internal float Lift;

		internal readonly List<Corner3> Tris = new List<Corner3>();

		internal Vector3[] Buf;

		internal Vector3 LastCam = Vector3.positiveInfinity;

		internal bool LastTess;

		internal int LastFrame = -1;

		internal float[] LastFactors;

		internal float LastTessAmount = -1f;

		internal float LastWater = float.NaN;

		internal bool LastNear;

		internal uint LastMask = uint.MaxValue;
	}

	internal struct Corner3
	{
		internal Vector3 P0;

		internal Vector3 P1;

		internal Vector3 P2;

		internal Color C0;

		internal Color C1;

		internal Color C2;

		internal Vector2 UV0;

		internal Vector2 UV1;

		internal Vector2 UV2;
	}

	private static readonly FieldRef<Heightmap, Mesh> RenderMesh = AccessTools.FieldRefAccess<Heightmap, Mesh>("m_renderMesh");

	private static readonly FieldRef<Heightmap, Texture2D> PaintMask = AccessTools.FieldRefAccess<Heightmap, Texture2D>("m_paintMask");

	private static readonly FieldRef<Heightmap, Material> MaterialInstance = AccessTools.FieldRefAccess<Heightmap, Material>("m_materialInstance");

	internal static readonly List<Sheet> All = new List<Sheet>();

	private static readonly Tess tess = new Tess();

	private const int BinN = 8;

	internal static Sheet Make(Heightmap hm, Mesh mesh, Renderer r, Transform space, float lift, List<(int vert, int i0, int i1, int i2, Vector3 bary, Vector3 baseInSpace, Vector3 normalInSpace)> on)
	{
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0260: Unknown result type (might be due to invalid IL or missing references)
		//IL_026c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0276: Unknown result type (might be due to invalid IL or missing references)
		//IL_027b: Unknown result type (might be due to invalid IL or missing references)
		//IL_028b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0290: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_017e: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01af: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0204: Unknown result type (might be due to invalid IL or missing references)
		//IL_0213: Unknown result type (might be due to invalid IL or missing references)
		//IL_0218: Unknown result type (might be due to invalid IL or missing references)
		//IL_0227: Unknown result type (might be due to invalid IL or missing references)
		//IL_022c: Unknown result type (might be due to invalid IL or missing references)
		Mesh val = (Object.op_Implicit((Object)(object)hm) ? RenderMesh.Invoke(hm) : null);
		if (!Object.op_Implicit((Object)(object)hm) || !Object.op_Implicit((Object)(object)val) || !val.isReadable || on.Count == 0)
		{
			return null;
		}
		Vector3[] vertices = val.vertices;
		Color[] colors = val.colors;
		Vector2[] uv = val.uv;
		if (colors == null || colors.Length != vertices.Length || uv == null || uv.Length != vertices.Length)
		{
			return null;
		}
		Sheet sheet = new Sheet
		{
			Mesh = mesh,
			R = r,
			Hm = hm,
			Space = space,
			Lift = lift,
			Verts = new int[on.Count],
			Tri = new int[on.Count],
			Domain = (Vector2[])(object)new Vector2[on.Count],
			Base = (Vector3[])(object)new Vector3[on.Count],
			Normal = (Vector3[])(object)new Vector3[on.Count]
		};
		Dictionary<(int, int, int), int> dictionary = new Dictionary<(int, int, int), int>();
		Matrix4x4 localToWorldMatrix = ((Component)hm).transform.localToWorldMatrix;
		for (int i = 0; i < on.Count; i++)
		{
			(int, int, int, int, Vector3, Vector3, Vector3) tuple = on[i];
			(int, int, int) key = (tuple.Item2, tuple.Item3, tuple.Item4);
			if (!dictionary.TryGetValue(key, out var value))
			{
				value = (dictionary[key] = sheet.Tris.Count);
				sheet.Tris.Add(new Corner3
				{
					P0 = ((Matrix4x4)(ref localToWorldMatrix)).MultiplyPoint3x4(vertices[tuple.Item2]),
					P1 = ((Matrix4x4)(ref localToWorldMatrix)).MultiplyPoint3x4(vertices[tuple.Item3]),
					P2 = ((Matrix4x4)(ref localToWorldMatrix)).MultiplyPoint3x4(vertices[tuple.Item4]),
					C0 = colors[tuple.Item2],
					C1 = colors[tuple.Item3],
					C2 = colors[tuple.Item4],
					UV0 = uv[tuple.Item2],
					UV1 = uv[tuple.Item3],
					UV2 = uv[tuple.Item4]
				});
			}
			sheet.Verts[i] = tuple.Item1;
			sheet.Tri[i] = value;
			sheet.Domain[i] = new Vector2(tuple.Item5.x, tuple.Item5.y);
			sheet.Base[i] = tuple.Item6;
			sheet.Normal[i] = tuple.Item7;
		}
		sheet.Buf = mesh.vertices;
		All.Add(sheet);
		Refresh(sheet, force: true);
		return sheet;
	}

	internal static void Remove(Sheet s)
	{
		if (s != null)
		{
			All.Remove(s);
		}
	}

	internal static void Update()
	{
		for (int num = All.Count - 1; num >= 0; num--)
		{
			Sheet sheet = All[num];
			if (sheet == null || !Object.op_Implicit((Object)(object)sheet.Mesh) || !Object.op_Implicit((Object)(object)sheet.Hm))
			{
				All.RemoveAt(num);
			}
			else if (!Object.op_Implicit((Object)(object)sheet.R) || sheet.R.isVisible)
			{
				Refresh(sheet, force: false);
			}
		}
	}

	private static void Refresh(Sheet s, bool force)
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_014d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_0166: Unknown result type (might be due to invalid IL or missing references)
		//IL_0175: Unknown result type (might be due to invalid IL or missing references)
		//IL_017a: Unknown result type (might be due to invalid IL or missing references)
		//IL_024f: 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_0270: Unknown result type (might be due to invalid IL or missing references)
		//IL_0263: Unknown result type (might be due to invalid IL or missing references)
		//IL_0275: Unknown result type (might be due to invalid IL or missing references)
		//IL_0279: Unknown result type (might be due to invalid IL or missing references)
		//IL_027e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0283: Unknown result type (might be due to invalid IL or missing references)
		//IL_0543: Unknown result type (might be due to invalid IL or missing references)
		//IL_0566: Unknown result type (might be due to invalid IL or missing references)
		//IL_056b: Unknown result type (might be due to invalid IL or missing references)
		//IL_056f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0574: Unknown result type (might be due to invalid IL or missing references)
		//IL_0581: Unknown result type (might be due to invalid IL or missing references)
		//IL_058c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0591: Unknown result type (might be due to invalid IL or missing references)
		//IL_0596: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03be: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03da: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0402: Unknown result type (might be due to invalid IL or missing references)
		//IL_0407: Unknown result type (might be due to invalid IL or missing references)
		//IL_040c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0410: Unknown result type (might be due to invalid IL or missing references)
		//IL_0417: Unknown result type (might be due to invalid IL or missing references)
		//IL_041e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0425: Unknown result type (might be due to invalid IL or missing references)
		//IL_042a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0431: Unknown result type (might be due to invalid IL or missing references)
		//IL_0438: Unknown result type (might be due to invalid IL or missing references)
		//IL_043d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0442: Unknown result type (might be due to invalid IL or missing references)
		//IL_0448: Unknown result type (might be due to invalid IL or missing references)
		//IL_044c: Unknown result type (might be due to invalid IL or missing references)
		//IL_044e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0458: Unknown result type (might be due to invalid IL or missing references)
		//IL_045a: Unknown result type (might be due to invalid IL or missing references)
		//IL_045e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0460: Unknown result type (might be due to invalid IL or missing references)
		//IL_0465: Unknown result type (might be due to invalid IL or missing references)
		Camera mainCamera = Utils.GetMainCamera();
		if (!Object.op_Implicit((Object)(object)mainCamera))
		{
			return;
		}
		Vector3 position = ((Component)mainCamera).transform.position;
		bool flag = Shader.IsKeywordEnabled("TESSELATION_ON");
		if (!force && s.LastFrame == Time.frameCount)
		{
			return;
		}
		if (!force && flag == s.LastTess)
		{
			Vector3 val = position - s.LastCam;
			if (((Vector3)(ref val)).sqrMagnitude < 0.0001f)
			{
				return;
			}
		}
		s.LastCam = position;
		s.LastTess = flag;
		s.LastFrame = Time.frameCount;
		Material val2 = MaterialInstance.Invoke(s.Hm);
		float num = ((Object.op_Implicit((Object)(object)val2) && val2.HasProperty("_Tess")) ? val2.GetFloat("_Tess") : 4f);
		float num2 = ((Object.op_Implicit((Object)(object)val2) && val2.HasProperty("_WaterLevel")) ? val2.GetFloat("_WaterLevel") : 30f);
		float[] array = new float[s.Tris.Count * 3];
		bool flag2 = true;
		for (int i = 0; i < s.Tris.Count; i++)
		{
			Corner3 corner = s.Tris[i];
			array[3 * i] = Factor(corner.P0, position);
			array[3 * i + 1] = Factor(corner.P1, position);
			array[3 * i + 2] = Factor(corner.P2, position);
			if (Flat(corner.P0, position) >= 100f || Flat(corner.P1, position) >= 100f || Flat(corner.P2, position) >= 100f)
			{
				flag2 = false;
			}
		}
		Texture2D val3 = PaintMask.Invoke(s.Hm);
		uint num3 = (Object.op_Implicit((Object)(object)val3) ? ((Texture)val3).updateCount : 0u);
		if (!force && flag2 && s.LastNear && num == s.LastTessAmount && num2 == s.LastWater && num3 == s.LastMask && Same(array, s.LastFactors))
		{
			return;
		}
		s.LastFactors = array;
		s.LastNear = flag2;
		s.LastTessAmount = num;
		s.LastWater = num2;
		s.LastMask = num3;
		Texture2D mask = PaintMask.Invoke(s.Hm);
		Matrix4x4 worldToLocalMatrix = ((Component)s.Hm).transform.worldToLocalMatrix;
		Matrix4x4 val4 = (Object.op_Implicit((Object)(object)s.Space) ? s.Space.worldToLocalMatrix : Matrix4x4.identity);
		Vector3 val5 = ((Matrix4x4)(ref val4)).MultiplyVector(Vector3.up);
		(float[], float[], float[], int[], int, List<int>[])[] array2 = new(float[], float[], float[], int[], int, List<int>[])[s.Tris.Count];
		for (int j = 0; j < s.Tris.Count; j++)
		{
			Corner3 corner2 = s.Tris[j];
			if (flag)
			{
				float num4 = array[3 * j];
				float num5 = array[3 * j + 1];
				float num6 = array[3 * j + 2];
				float num7 = num4 * num;
				float num8 = num5 * num;
				float num9 = num6 * num;
				tess.TessellateTri((num8 + num9) * 0.5f, (num7 + num9) * 0.5f, (num7 + num8) * 0.5f, (num7 + num8 + num9) * 0.333333f);
			}
			else
			{
				tess.TessellateTri(1f, 1f, 1f, 1f);
			}
			int numPoints = tess.NumPoints;
			float[] array3 = new float[numPoints];
			float[] array4 = new float[numPoints];
			float[] array5 = new float[numPoints];
			for (int k = 0; k < numPoints; k++)
			{
				float num10 = tess.U[k];
				float num11 = tess.V[k];
				float num12 = 1f - num10 - num11;
				array3[k] = num10;
				array4[k] = num11;
				Vector3 val6 = corner2.P1 * num11 + corner2.P0 * num10 + corner2.P2 * num12;
				Color c = corner2.C1 * num11 + corner2.C0 * num10 + corner2.C2 * num12;
				Vector2 uv = corner2.UV1 * num11 + corner2.UV0 * num10 + corner2.UV2 * num12;
				array5[k] = Offset(val6, ((Matrix4x4)(ref worldToLocalMatrix)).MultiplyPoint3x4(val6).y, c, uv, Sample(mask, uv), position, num2, flag);
			}
			int[] array6 = new int[tess.NumIndices];
			Array.Copy(tess.Index, array6, tess.NumIndices);
			array2[j] = (array3, array4, array5, array6, tess.NumIndices, Bins(array3, array4, array6, tess.NumIndices));
		}
		for (int l = 0; l < s.Verts.Length; l++)
		{
			(float[], float[], float[], int[], int, List<int>[]) tuple = array2[s.Tri[l]];
			float num13 = Interpolate(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4, tuple.Item5, tuple.Item6, s.Domain[l]);
			s.Buf[s.Verts[l]] = s.Base[l] + val5 * num13 + s.Normal[l] * s.Lift;
		}
		s.Mesh.vertices = s.Buf;
		s.Mesh.RecalculateBounds();
	}

	private static float Factor(Vector3 corner, Vector3 cam)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		return Mathf.Min(Mathf.Max((0f - (Vector3.Distance(corner, cam) - 10f)) * 0.025f + 1f, 0.01f), 1f);
	}

	private static Color Sample(Texture2D mask, Vector2 uv)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)mask) || !((Texture)mask).isReadable)
		{
			return new Color(0f, 0f, 0f, 1f);
		}
		int w = ((Texture)mask).width;
		int h = ((Texture)mask).height;
		float num = uv.x * (float)w - 0.5f;
		float num2 = uv.y * (float)h - 0.5f;
		int num3 = Mathf.FloorToInt(num);
		int num4 = Mathf.FloorToInt(num2);
		float num5 = num - (float)num3;
		float num6 = num2 - (float)num4;
		Color val = Color.Lerp(P(num3, num4), P(num3 + 1, num4), num5);
		Color val2 = Color.Lerp(P(num3, num4 + 1), P(num3 + 1, num4 + 1), num5);
		return Color.Lerp(val, val2, num6);
		Color P(int i, int j)
		{
			//IL_0024: 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_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_0034: 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)
			Color pixel = mask.GetPixel(Mathf.Clamp(i, 0, w - 1), Mathf.Clamp(j, 0, h - 1));
			Color linear = ((Color)(ref pixel)).linear;
			linear.a = pixel.a;
			return linear;
		}
	}

	private static float Sat(float x)
	{
		return Mathf.Clamp01(x);
	}

	private static float Offset(Vector3 world, float localY, Color c, Vector2 uv, Color m, Vector3 cam, float water, bool tessOn)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: 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_0056: 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_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: 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_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_011a: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0224: Unknown result type (might be due to invalid IL or missing references)
		//IL_022a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0232: 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_0240: Unknown result type (might be due to invalid IL or missing references)
		//IL_0245: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: 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_0193: 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_01d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_020b: Unknown result type (might be due to invalid IL or missing references)
		//IL_016c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		float num = Mathf.Max(1f - Mathf.Max(Mathf.Max(Mathf.Abs(c.r), Mathf.Abs(c.g - 1f)), Mathf.Max(Mathf.Abs(c.b), Mathf.Abs(c.a))), 0f);
		float num2 = Mathf.Max(1f - Mathf.Max(Mathf.Max(Mathf.Abs(c.g), Mathf.Abs(c.r - 1f)), Mathf.Max(Mathf.Abs(c.b), Mathf.Abs(c.a - 1f))), 0f);
		float num3 = Mathf.Max(1f - Mathf.Max(Mathf.Max(Mathf.Abs(c.r), Mathf.Abs(c.g)), Mathf.Max(Mathf.Abs(c.b - 1f), Mathf.Abs(c.a - 1f))), 0f);
		float num4 = num * (1f - Sat(m.b * 10f + (m.r + m.g)));
		float num5 = num4 * 0.1f + num2 * 0.1f + num2 * m.a * 0.8f;
		if (tessOn)
		{
			bool num6 = uv.x < 0.001f || uv.y < 0.001f || uv.x > 0.999f || uv.y > 0.999f;
			float num7 = (0.25f - 0.15f * Sat(m.b * 10f + m.r * 4f)) * (1f - num3) * Mathf.Min(1f, 1f - num4);
			float num8 = Mathf.Sin(1.4f * world.x) * Mathf.Cos(1.5f * world.z) * Mathf.Sin(2.235f * world.x) * Mathf.Cos(2.435f * world.z);
			if (!num6)
			{
				num5 += num8 * num7;
			}
		}
		Vector2 val = new Vector2(world.x - cam.x, world.z - cam.z);
		float num9 = Sat((((Vector2)(ref val)).magnitude - 100f) * 0.005f);
		num9 = num9 * num9 * (3f - 2f * num9);
		return num5 + ((localY + num5 > water) ? num9 : (0f - num9));
	}

	private static float Flat(Vector3 p, Vector3 cam)
	{
		//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_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		Vector2 val = new Vector2(p.x - cam.x, p.z - cam.z);
		return ((Vector2)(ref val)).magnitude;
	}

	private static bool Same(float[] a, float[] b)
	{
		if (b == null || a.Length != b.Length)
		{
			return false;
		}
		for (int i = 0; i < a.Length; i++)
		{
			if (a[i] != b[i])
			{
				return false;
			}
		}
		return true;
	}

	private static List<int>[] Bins(float[] u, float[] v, int[] idx, int n)
	{
		List<int>[] array = new List<int>[64];
		for (int i = 0; i + 2 < n; i += 3)
		{
			int num = idx[i];
			int num2 = idx[i + 1];
			int num3 = idx[i + 2];
			float num4 = Mathf.Min(u[num], Mathf.Min(u[num2], u[num3])) - 1E-05f;
			float num5 = Mathf.Max(u[num], Mathf.Max(u[num2], u[num3])) + 1E-05f;
			float num6 = Mathf.Min(v[num], Mathf.Min(v[num2], v[num3])) - 1E-05f;
			float num7 = Mathf.Max(v[num], Mathf.Max(v[num2], v[num3])) + 1E-05f;
			int num8 = Mathf.Clamp((int)(num4 * 8f), 0, 7);
			int num9 = Mathf.Clamp((int)(num5 * 8f), 0, 7);
			int num10 = Mathf.Clamp((int)(num6 * 8f), 0, 7);
			int num11 = Mathf.Clamp((int)(num7 * 8f), 0, 7);
			for (int j = num10; j <= num11; j++)
			{
				for (int k = num8; k <= num9; k++)
				{
					(array[j * 8 + k] ?? (array[j * 8 + k] = new List<int>())).Add(i);
				}
			}
		}
		return array;
	}

	private static float Interpolate(float[] u, float[] v, float[] dy, int[] idx, int n, List<int>[] bins, Vector2 p)
	{
		//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)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		int num = (int)Mathf.Floor(p.x * 8f);
		int num2 = (int)Mathf.Floor(p.y * 8f);
		if (num >= 0 && num2 >= 0 && num < 8 && num2 < 8)
		{
			List<int> list = bins[num2 * 8 + num];
			if (list != null)
			{
				foreach (int item in list)
				{
					int num3 = idx[item];
					int num4 = idx[item + 1];
					int num5 = idx[item + 2];
					float num6 = u[num3];
					float num7 = v[num3];
					float num8 = u[num4];
					float num9 = v[num4];
					float num10 = u[num5];
					float num11 = v[num5];
					float num12 = (num9 - num11) * (num6 - num10) + (num10 - num8) * (num7 - num11);
					if (!(Mathf.Abs(num12) < 1E-12f))
					{
						float num13 = ((num9 - num11) * (p.x - num10) + (num10 - num8) * (p.y - num11)) / num12;
						float num14 = ((num11 - num7) * (p.x - num10) + (num6 - num10) * (p.y - num11)) / num12;
						float num15 = 1f - num13 - num14;
						if (Mathf.Min(num13, Mathf.Min(num14, num15)) >= 0f)
						{
							return num13 * dy[num3] + num14 * dy[num4] + num15 * dy[num5];
						}
					}
				}
			}
		}
		return Interpolate(u, v, dy, idx, n, p);
	}

	private static float Interpolate(float[] u, float[] v, float[] dy, int[] idx, int n, Vector2 p)
	{
		//IL_0076: 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_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		float num = float.NegativeInfinity;
		float result = 0f;
		for (int i = 0; i + 2 < n; i += 3)
		{
			int num2 = idx[i];
			int num3 = idx[i + 1];
			int num4 = idx[i + 2];
			float num5 = u[num2];
			float num6 = v[num2];
			float num7 = u[num3];
			float num8 = v[num3];
			float num9 = u[num4];
			float num10 = v[num4];
			float num11 = (num8 - num10) * (num5 - num9) + (num9 - num7) * (num6 - num10);
			if (Mathf.Abs(num11) < 1E-12f)
			{
				continue;
			}
			float num12 = ((num8 - num10) * (p.x - num9) + (num9 - num7) * (p.y - num10)) / num11;
			float num13 = ((num10 - num6) * (p.x - num9) + (num5 - num9) * (p.y - num10)) / num11;
			float num14 = 1f - num12 - num13;
			float num15 = Mathf.Min(num12, Mathf.Min(num13, num14));
			if (num15 > num)
			{
				num = num15;
				result = num12 * dy[num2] + num13 * dy[num3] + num14 * dy[num4];
				if (num15 >= 0f)
				{
					return result;
				}
			}
		}
		return result;
	}
}
internal static class Look
{
	internal static readonly Color RedOchre = new Color(0.62f, 0.12f, 0.06f, 1f);

	internal static readonly Color Ember = new Color(1f, 0.267f, 0f, 1f);

	internal const float HaloFaint = 0.4f;

	private static Material halo;

	private static bool haloLooked;

	private static Material leaf;

	private static bool looked;

	private static readonly Dictionary<string, Material> materials = new Dictionary<string, Material>();

	private static readonly Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>();

	internal static Texture2D Texture(Rune rune)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Expected O, but got Unknown
		if (textures.TryGetValue(rune.Art, out var value))
		{
			return value;
		}
		byte[] array = Runes.ArtBytes(rune);
		if (array == null)
		{
			return null;
		}
		value = new Texture2D(2, 2, (TextureFormat)4, true)
		{
			name = "PotatoPainter " + rune.Art,
			wrapMode = (TextureWrapMode)1
		};
		ImageConversion.LoadImage(value, array);
		KeepCoverage(value, 0.5f);
		textures[rune.Art] = value;
		return value;
	}

	internal static Material Material(Rune rune, bool etched)
	{
		//IL_0072: 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_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_0067: Unknown result type (might be due to invalid IL or missing references)
		string text = (etched ? "etched " : "") + rune.Art;
		if (materials.TryGetValue(text, out var value))
		{
			return value;
		}
		Texture2D val = Texture(rune);
		if (!Object.op_Implicit((Object)(object)Leaf()) || !Object.op_Implicit((Object)(object)val))
		{
			return null;
		}
		value = (Material)(etched ? ((object)Paint(text, val, RedOchre, Ember, 1f)) : ((object)new Material(leaf)
		{
			name = "PotatoPainter " + text,
			mainTexture = (Texture)(object)val
		}));
		if (etched && Object.op_Implicit((Object)(object)value) && value.HasProperty("_BumpMap"))
		{
			value.SetTexture("_BumpMap", (Texture)null);
		}
		materials[text] = value;
		return value;
	}

	internal static Material HaloFor(Rune rune)
	{
		//IL_0040: 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)
		string key = "halo " + rune.Art;
		if (materials.TryGetValue(key, out var value))
		{
			return value;
		}
		Texture2D val = Texture(rune);
		value = (Object.op_Implicit((Object)(object)val) ? Halo(rune.Art, HaloTexture(val), Ember * 0.4f) : null);
		materials[key] = value;
		return value;
	}

	private static Texture2D HaloTexture(Texture2D rune)
	{
		//IL_0218: Unknown result type (might be due to invalid IL or missing references)
		//IL_021d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0234: Unknown result type (might be due to invalid IL or missing references)
		//IL_0239: Unknown result type (might be due to invalid IL or missing references)
		//IL_0240: Unknown result type (might be due to invalid IL or missing references)
		//IL_0247: Unknown result type (might be due to invalid IL or missing references)
		//IL_025d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0265: Unknown result type (might be due to invalid IL or missing references)
		//IL_026e: Expected O, but got Unknown
		int width = ((Texture)rune).width;
		int height = ((Texture)rune).height;
		Color32[] pixels = rune.GetPixels32(0);
		float[] array = new float[width * height];
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = (float)(int)pixels[i].a / 255f;
		}
		float num = 7f * (float)width / 256f;
		int num2 = Mathf.CeilToInt(num * 3f);
		float[] array2 = new float[num2 * 2 + 1];
		float num3 = 0f;
		for (int j = -num2; j <= num2; j++)
		{
			array2[j + num2] = Mathf.Exp((float)(-j * j) / (2f * num * num));
			num3 += array2[j + num2];
		}
		for (int k = 0; k < array2.Length; k++)
		{
			array2[k] /= num3;
		}
		float[] array3 = new float[width * height];
		float[] array4 = new float[width * height];
		for (int l = 0; l < height; l++)
		{
			for (int m = 0; m < width; m++)
			{
				float num4 = 0f;
				for (int n = -num2; n <= num2; n++)
				{
					int num5 = m + n;
					if (num5 >= 0 && num5 < width)
					{
						num4 += array[l * width + num5] * array2[n + num2];
					}
				}
				array3[l * width + m] = num4;
			}
		}
		for (int num6 = 0; num6 < height; num6++)
		{
			for (int num7 = 0; num7 < width; num7++)
			{
				float num8 = 0f;
				for (int num9 = -num2; num9 <= num2; num9++)
				{
					int num10 = num6 + num9;
					if (num10 >= 0 && num10 < height)
					{
						num8 += array3[num10 * width + num7] * array2[num9 + num2];
					}
				}
				array4[num6 * width + num7] = num8;
			}
		}
		Color32[] array5 = (Color32[])(object)new Color32[width * height];
		for (int num11 = 0; num11 < array5.Length; num11++)
		{
			array5[num11] = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)Mathf.RoundToInt(Mathf.Clamp01(array4[num11] * 1.6f) * (1f - array[num11]) * 255f));
		}
		Texture2D val = new Texture2D(width, height, (TextureFormat)4, true)
		{
			wrapMode = (TextureWrapMode)1,
			filterMode = (FilterMode)1,
			name = "PotatoPainter halo " + ((Object)rune).name
		};
		val.SetPixels32(array5);
		val.Apply(true, false);
		return val;
	}

	internal static Material Paint(string name, Texture2D tex, Color col, Color glowCol, float glow)
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)Leaf()) || !Object.op_Implicit((Object)(object)tex))
		{
			return null;
		}
		Material val = new Material(leaf)
		{
			name = "PotatoPainter " + name,
			mainTexture = (Texture)(object)tex
		};
		if (val.HasProperty("_Color"))
		{
			val.SetColor("_Color", col);
		}
		if (glow > 0f && val.HasProperty("_EmissionColor"))
		{
			if (val.HasProperty("_EmissiveTex"))
			{
				val.SetTexture("_EmissiveTex", (Texture)(object)tex);
			}
			val.SetColor("_EmissionColor", glowCol * glow);
			val.EnableKeyword("_EMISSION");
			val.globalIlluminationFlags = (MaterialGlobalIlluminationFlags)1;
		}
		return val;
	}

	internal static Material Halo(string name, Texture2D tex, Color col)
	{
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_00e9: 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)
		if (!haloLooked)
		{
			haloLooked = true;
			List<Material> list = Resources.FindObjectsOfTypeAll<Material>().Where(Additive).ToList();
			halo = ((IEnumerable<Material>)list).FirstOrDefault((Func<Material, bool>)((Material x) => ((Object)x).name == "embers")) ?? ((IEnumerable<Material>)list).FirstOrDefault((Func<Material, bool>)((Material x) => x.shaderKeywords.Length == 1 && x.shaderKeywords[0] == "_ALPHABLEND_ON" && x.GetFloat("_SoftParticlesEnabled") == 0f));
			if (!Object.op_Implicit((Object)(object)halo))
			{
				Plugin.Log.LogWarning((object)string.Format("Glow halo off this session: none of the game's additive {0} materials is loaded ({1} found).", "Particles/Standard Unlit2", list.Count));
			}
		}
		if (!Object.op_Implicit((Object)(object)halo) || !Object.op_Implicit((Object)(object)tex))
		{
			return null;
		}
		Material val = new Material(halo)
		{
			name = "PotatoPainter halo " + name,
			mainTexture = (Texture)(object)tex
		};
		Color val2 = col;
		val2.a = 1f;
		if (val.HasProperty("_Color"))
		{
			val.SetColor("_Color", val2);
		}
		return val;
		static bool Additive(Material x)
		{
			if (Object.op_Implicit((Object)(object)x) && Object.op_Implicit((Object)(object)x.shader) && ((Object)x.shader).name == "Particles/Standard Unlit2" && x.HasProperty("_DstBlend") && x.GetFloat("_DstBlend") == 1f && x.HasProperty("_SrcBlend"))
			{
				return x.GetFloat("_SrcBlend") == 5f;
			}
			return false;
		}
	}

	private static Material Leaf()
	{
		//IL_0098: 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_00ad: Expected O, but got Unknown
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		if (looked)
		{
			return leaf;
		}
		looked = true;
		GameObject prefab = PrefabManager.Instance.GetPrefab("Beech1");
		Material val = (Object.op_Implicit((Object)(object)prefab) ? prefab.GetComponentsInChildren<Renderer>(true).SelectMany((Renderer r) => r.sharedMaterials).FirstOrDefault((Func<Material, bool>)((Material x) => Object.op_Implicit((Object)(object)x) && Object.op_Implicit((Object)(object)x.shader) && ((Object)x.shader).name == "Custom/Vegetation")) : null);
		if (!Object.op_Implicit((Object)(object)val))
		{
			Plugin.Log.LogError((object)"The beech's leaf material wasn't found; paint can't be drawn this session.");
			return null;
		}
		leaf = new Material(val)
		{
			name = "PotatoPainter paint"
		};
		if (leaf.HasProperty("_Color"))
		{
			leaf.SetColor("_Color", RedOchre);
		}
		if (leaf.HasProperty("_Cutoff"))
		{
			leaf.SetFloat("_Cutoff", 0.5f);
		}
		string[] array = new string[5] { "_SwaySpeed", "_SwayDistance", "_RippleDistance", "_RippleSpeed", "_PushDistance" };
		foreach (string text in array)
		{
			if (leaf.HasProperty(text))
			{
				leaf.SetFloat(text, 0f);
			}
		}
		leaf.DisableKeyword("_EMISSION");
		leaf.globalIlluminationFlags = (MaterialGlobalIlluminationFlags)4;
		return leaf;
	}

	internal static void Colour(Renderer r, Color col, Color glow)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Expected O, but got Unknown
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: 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)
		MaterialPropertyBlock val = new MaterialPropertyBlock();
		val.SetColor("_Color", new Color(col.r, col.g, col.b, 1f));
		if (((Color)(ref glow)).maxColorComponent > 0f)
		{
			val.SetColor("_EmissionColor", new Color(glow.r, glow.g, glow.b, 1f));
		}
		r.SetPropertyBlock(val);
	}

	internal static void HaloColour(Renderer r, Color glow)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Expected O, but got Unknown
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		MaterialPropertyBlock val = new MaterialPropertyBlock();
		Color val2 = glow * 0.4f;
		val2.a = 1f;
		val.SetColor("_Color", val2);
		r.SetPropertyBlock(val);
	}

	internal static void KeepCoverage(Texture2D t, float cutoff)
	{
		float num = Cover(t.GetPixels(0), 1f);
		for (int i = 1; i < ((Texture)t).mipmapCount; i++)
		{
			Color[] pixels = t.GetPixels(i);
			float num2 = 0.25f;
			float num3 = 8f;
			for (int j = 0; j < 24; j++)
			{
				float num4 = (num2 + num3) * 0.5f;
				if (Cover(pixels, num4) < num)
				{
					num2 = num4;
				}
				else
				{
					num3 = num4;
				}
			}
			for (int k = 0; k < pixels.Length; k++)
			{
				pixels[k].a = Mathf.Clamp01(pixels[k].a * num3);
			}
			t.SetPixels(pixels, i);
		}
		t.Apply(false);
		float Cover(Color[] px, float num6)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			int num5 = 0;
			for (int l = 0; l < px.Length; l++)
			{
				if (px[l].a * num6 >= cutoff)
				{
					num5++;
				}
			}
			return (float)num5 / (float)px.Length;
		}
	}

	internal static Sprite Icon(Rune rune, bool etched)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		//IL_0024: 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_0040: Expected O, but got Unknown
		//IL_0068: 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_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01db: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0232: Unknown result type (might be due to invalid IL or missing references)
		//IL_0241: Unknown result type (might be due to invalid IL or missing references)
		byte[] array = Runes.ArtBytes(rune);
		if (array == null)
		{
			return null;
		}
		Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
		ImageConversion.LoadImage(val, array);
		Texture2D val2 = new Texture2D(64, 64, (TextureFormat)4, false)
		{
			name = "PotatoPainter icon " + rune.Art
		};
		Color val3 = (etched ? new Color(1f, 0.55f, 0.2f) : new Color(0.86f, 0.34f, 0.18f));
		float[] array2 = new float[4096];
		for (int i = 0; i < 64; i++)
		{
			for (int j = 0; j < 64; j++)
			{
				array2[i * 64 + j] = val.GetPixelBilinear(((float)j + 0.5f) / 64f, ((float)i + 0.5f) / 64f).a;
			}
		}
		float[] array3 = new float[4096];
		if (etched)
		{
			for (int k = 0; k < 64; k++)
			{
				for (int l = 0; l < 64; l++)
				{
					float num = 0f;
					float num2 = 0f;
					for (int m = -2; m <= 2; m++)
					{
						for (int n = -2; n <= 2; n++)
						{
							int num3 = l + n;
							int num4 = k + m;
							float num5 = 1f / (1f + (float)(n * n) + (float)(m * m));
							num2 += num5;
							if (num3 >= 0 && num3 < 64 && num4 >= 0 && num4 < 64)
							{
								num += array2[num4 * 64 + num3] * num5;
							}
						}
					}
					array3[k * 64 + l] = Mathf.Clamp01(num / num2 * 1.5f) * 0.35f;
				}
			}
		}
		Color[] array4 = (Color[])(object)new Color[4096];
		for (int num6 = 0; num6 < array4.Length; num6++)
		{
			array4[num6] = new Color(val3.r, val3.g, val3.b, Mathf.Max(array2[num6], array3[num6]));
		}
		val2.SetPixels(array4);
		val2.Apply(false);
		Object.Destroy((Object)(object)val);
		return Sprite.Create(val2, new Rect(0f, 0f, 64f, 64f), new Vector2(0.5f, 0.5f));
	}
}
public sealed class Paint : MonoBehaviour, IPlaced
{
	public string Art;

	public bool Etched;

	internal const float DefaultSize = 0.5f;

	internal static readonly List<Paint> Loaded = new List<Paint>();

	private static readonly int KVersion = StringExtensionMethods.GetStableHashCode("PotatoPainter.Version");

	private static readonly int KCenter = StringExtensionMethods.GetStableHashCode("PotatoPainter.Center");

	private static readonly int KNormal = StringExtensionMethods.GetStableHashCode("PotatoPainter.Normal");

	private static readonly int KUp = StringExtensionMethods.GetStableHashCode("PotatoPainter.Up");

	private static readonly int KTree = StringExtensionMethods.GetStableHashCode("PotatoPainter.Tree");

	private static readonly int KSize = StringExtensionMethods.GetStableHashCode("PotatoPainter.Size");

	private static readonly int KColor = StringExtensionMethods.GetStableHashCode("PotatoPainter.Color");

	private static readonly int KLayer = StringExtensionMethods.GetStableHashCode("PotatoPainter.Layer");

	private static readonly int KGlow = StringExtensionMethods.GetStableHashCode("PotatoPainter.Glow");

	private static readonly int KLight = StringExtensionMethods.GetStableHashCode("PotatoPainter.Light");

	private static readonly int KGlowShare = StringExtensionMethods.GetStableHashCode("PotatoPainter.GlowShare");

	private static Frame aim;

	private static bool aimValid;

	private static bool aimFar;

	private static float aimTime;

	private static int aimLayer;

	private static float previewCost;

	private static Rune aimRune;

	private static bool aimEtched;

	private static float aimSize = 0.5f;

	private static Color aimCol;

	private static Color aimGlow;

	private static bool aimLit;

	private const float RepeatShare = 0.15f;

	internal Rune Rune;

	internal Frame Frame;

	internal float Size;

	internal Color Col;

	internal Color Glow;

	internal int Layer;

	internal bool Lit;

	internal bool GlowShare;

	internal float Dim = 1f;

	private float nextSun;

	internal bool HasData;

	private ZNetView nview;

	private bool ghost;

	private bool listed;

	private bool placedWithoutAim;

	private bool repeat;

	private Built built;

	private Built preview;

	private Trace.Job job;

	private Light previewLight;

	private float nextCheck;

	private float previewTime;

	private Vector3 lastEye = Vector3.positiveInfinity;

	private Vector3 lastDir;

	private float lastTurn;

	private int lastSettings = -1;

	private static readonly FieldRef<Player, int> PlaceRotation = AccessTools.FieldRefAccess<Player, int>("m_placeRotation");

	private static readonly FieldRef<Player, float> PlaceRotationDegrees = AccessTools.FieldRefAccess<Player, float>("m_placeRotationDegrees");

	private Vector3? ghostGrassAt;

	internal Color Emission
	{
		get
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			if (!Etched)
			{
				return Color.black;
			}
			Color result = GlowCap.Emission(Glow, GlowShare) * Dim;
			result.a = 1f;
			return result;
		}
	}

	internal Color DrawCol => Col;

	internal float Share
	{
		get
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			if (Etched)
			{
				if (!GlowShare)
				{
					return Mathf.Clamp01(((Color)(ref Glow)).maxColorComponent / Mathf.Max(GlowCap.Cap(Glow, GlowCap.Bloom), 0.001f));
				}
				return Mathf.Clamp01(((Color)(ref Glow)).maxColorComponent);
			}
			return 0f;
		}
	}

	internal Transform CenterHost
	{
		get
		{
			if (job == null || !Object.op_Implicit((Object)(object)job.Built.CenterHost))
			{
				return built?.CenterHost;
			}
			return job.Built.CenterHost;
		}
	}

	internal Built Drawn => built;

	private float Lift => 0.004f + 0.002f * (float)Layer;

	private void Awake()
	{
		nview = ((Component)this).GetComponent<ZNetView>();
		Rune = Runes.All.FirstOrDefault((Rune r) => r.Art == Art);
	}

	private void Start()
	{
		if (GUIManager.IsHeadless() || Rune == null)
		{
			((Behaviour)this).enabled = false;
			return;
		}
		ghost = Player.IsPlacementGhost(((Component)this).gameObject) || !Object.op_Implicit((Object)(object)nview) || nview.GetZDO() == null;
		if (!ghost)
		{
			Loaded.Add(this);
			listed = true;
			Load();
		}
	}

	public void OnPlaced()
	{
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: 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_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ab: 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)
		if (!Object.op_Implicit((Object)(object)nview) || nview.GetZDO() == null)
		{
			return;
		}
		if (!aimValid || Time.time - aimTime > 1f)
		{
			placedWithoutAim = true;
			return;
		}
		if (Loaded.Any((Paint p) => Object.op_Implicit((Object)(object)p) && p.HasData && Repeats(p, Rune, Etched, aim, aimSize, aimCol, aimGlow, aimLit, share: true)))
		{
			repeat = true;
			return;
		}
		aimLayer = LayerAt(aim.Center, aimSize, null);
		ZDO zDO = nview.GetZDO();
		zDO.Set(KCenter, aim.Center);
		zDO.Set(KNormal, aim.Normal);
		zDO.Set(KUp, aim.Up);
		zDO.Set(KTree, aim.Tree ? 1 : 0, false);
		zDO.Set(KSize, aimSize);
		zDO.Set(KColor, new Vector3(aimCol.r, aimCol.g, aimCol.b));
		zDO.Set(KLayer, aimLayer, false);
		if (Etched)
		{
			zDO.Set(KGlow, new Vector3(aimGlow.r, aimGlow.g, aimGlow.b));
			zDO.Set(KGlowShare, 1, false);
			zDO.Set(KLight, aimLit ? 1 : 2, false);
		}
		zDO.Set(KVersion, 1, false);
		zDO.SetPosition(aim.Center);
		((Component)this).transform.position = aim.Center;
	}

	private void Load()
	{
		//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_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: 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_016a: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_017c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		ZDO zDO = nview.GetZDO();
		if (zDO.GetInt(KVersion, 0) == 0)
		{
			if (!nview.IsOwner())
			{
				return;
			}
			if (repeat)
			{
				Player localPlayer = Player.m_localPlayer;
				if (localPlayer != null)
				{
					((Character)localPlayer).Message((MessageType)2, Etched ? "$msg_potato_etch_repeat" : "$msg_potato_paint_repeat", 0, (Sprite)null, false);
				}
			}
			else if (placedWithoutAim)
			{
				Player localPlayer2 = Player.m_localPlayer;
				if (localPlayer2 != null)
				{
					((Character)localPlayer2).Message((MessageType)2, Etched ? "$msg_potato_etch_nosurface" : "$msg_potato_paint_nosurface", 0, (Sprite)null, false);
				}
			}
			ZNetScene.instance.Destroy(((Component)this).gameObject);
			return;
		}
		Frame = new Frame
		{
			Center = zDO.GetVec3(KCenter, ((Component)this).transform.position),
			Normal = zDO.GetVec3(KNormal, Vector3.up),
			Up = zDO.GetVec3(KUp, Vector3.forward),
			Tree = (zDO.GetInt(KTree, 0) != 0)
		};
		Size = zDO.GetFloat(KSize, 0.5f);
		Vector3 vec = zDO.GetVec3(KColor, new Vector3(Look.RedOchre.r, Look.RedOchre.g, Look.RedOchre.b));
		Col = new Color(vec.x, vec.y, vec.z, 1f);
		Vector3 val = (Etched ? zDO.GetVec3(KGlow, new Vector3(Look.Ember.r, Look.Ember.g, Look.Ember.b)) : Vector3.zero);
		Glow = new Color(val.x, val.y, val.z, 1f);
		Lit = Etched && zDO.GetInt(KLight, 1) != 2;
		GlowShare = Etched && zDO.GetInt(KGlowShare, 0) == 1;
		Layer = zDO.GetInt(KLayer, 0);
		HasData = true;
		if (!ClearPile())
		{
			Rebuild();
		}
	}

	internal static string Refusal()
	{
		if (!aimValid && aimFar)
		{
			if (!aimEtched)
			{
				return "$msg_potato_paint_toofar";
			}
			return "$msg_potato_etch_toofar";
		}
		if (!aimValid || Time.time - aimTime > 1f)
		{
			if (!aimEtched)
			{
				return "$msg_potato_paint_nosurface";
			}
			return "$msg_potato_etch_nosurface";
		}
		if (aimRune != null && Loaded.Any((Paint p) => Object.op_Implicit((Object)(object)p) && p.HasData && Repeats(p, aimRune, aimEtched, aim, aimSize, aimCol, aimGlow, aimLit, share: true)))
		{
			if (!aimEtched)
			{
				return "$msg_potato_paint_repeat";
			}
			return "$msg_potato_etch_repeat";
		}
		return null;
	}

	private static bool Repeats(Paint p, Rune rune, bool etched, Frame f, float size, Color col, Color glow, bool lit, bool share)
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		if (p.Rune == rune && p.Etched == etched && Mathf.Abs(p.Size - size) < 0.01f && Near(p.Col, col) && Near(p.Glow, glow) && p.Lit == lit && (!etched || p.GlowShare == share) && Vector3.Distance(p.Frame.Center, f.Center) < Mathf.Min(p.Size, size) * 0.15f)
		{
			return Vector3.Dot(p.Frame.Normal, f.Normal) > 0.9f;
		}
		return false;
	}

	private static bool Near(Color a, Color b)
	{
		//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_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		if (Mathf.Abs(a.r - b.r) < 0.01f && Mathf.Abs(a.g - b.g) < 0.01f)
		{
			return Mathf.Abs(a.b - b.b) < 0.01f;
		}
		return false;
	}

	private bool ClearPile()
	{
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		List<Paint> list = Loaded.Where((Paint p) => Object.op_Implicit((Object)(object)p) && p.HasData && Repeats(p, Rune, Etched, Frame, Size, Col, Glow, Lit, GlowShare)).ToList();
		if (list.Count < 2)
		{
			return false;
		}
		Paint keep = list.OrderByDescending((Paint p) => p.Layer).First();
		HashSet<Paint> hashSet = new HashSet<Paint>(list.Where((Paint p) => (Object)(object)p != (Object)(object)keep));
		int num = 0;
		foreach (Paint item in Loaded)
		{
			if (Object.op_Implicit((Object)(object)item) && item.HasData && !list.Contains(item) && item.Layer < keep.Layer && Vector3.Distance(item.Frame.Center, keep.Frame.Center) < (item.Size + keep.Size) * 0.5f * 0.9f)
			{
				num = Mathf.Max(num, item.Layer + 1);
			}
		}
		foreach (Paint item2 in hashSet)
		{
			item2.HasData = false;
			item2.Delete();
		}
		if (keep.Layer != num)
		{
			keep.SetLayer(num);
		}
		return hashSet.Contains(this);
	}

	private void SetLayer(int layer)
	{
		if (Object.op_Implicit((Object)(object)nview) && nview.IsValid())
		{
			nview.ClaimOwnership();
			nview.GetZDO().Set(KLayer, layer, false);
			Layer = layer;
			if (built != null || job != null)
			{
				Rebuild();
			}
		}
	}

	private void Rebuild()
	{
		//IL_0061: 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)
		if (Etched)
		{
			UpdateDim();
			nextSun = Time.time + 2f + Random.value;
		}
		job = Trace.Job.Prepare(Frame, Size, Lift, Look.Material(Rune, Etched), Etched ? Look.HaloFor(Rune) : null, DrawCol, Emission, preview: false, (Etched ? "etch " : "paint ") + Rune.Art);
		Trace.Run(job);
		nextCheck = Time.time + 2f + Random.value;
	}

	private void Apply(Trace.Job j)
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		Built built = j.Finish();
		this.built?.Clear();
		this.built = built;
		if (Etched && Lit && this.built.Sheets > 0)
		{
			this.built.Light = RuneLight.Make(Frame, Size, Emission, this.built.CenterHost, "etch " + Rune.Art);
			if (Object.op_Implicit((Object)(object)this.built.Light))
			{
				this.built.Objects.Add(this.built.Light);
			}
		}
		if (Etched)
		{
			Retint();
		}
		Grass.Set(this, this.built.OnGround, Frame, Size);
	}

	private bool UpdateDim()
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		float day = SunDim.Day();
		float sun = SunDim.Sun(Frame, Size);
		float num = SunDim.Factor(Glow, Share, day, sun, GlowCap.Bloom);
		if (Mathf.Abs(num - Dim) <= 0.01f)
		{
			return false;
		}
		Dim = num;
		return true;
	}

	internal bool RemakeLight()
	{
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		if (built == null || !Etched || !Lit || built.Sheets == 0)
		{
			return false;
		}
		if (Object.op_Implicit((Object)(object)built.Light))
		{
			built.Objects.Remove(built.Light);
			Object.Destroy((Object)(object)built.Light);
		}
		built.Light = RuneLight.Make(Frame, Size, Emission, built.CenterHost, "etch " + Rune.Art);
		if (Object.op_Implicit((Object)(object)built.Light))
		{
			built.Objects.Add(built.Light);
		}
		return Object.op_Implicit((Object)(object)built.Light);
	}

	internal bool Retint()
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_0086: 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)
		if (built == null || !Etched)
		{
			return false;
		}
		Color emission = Emission;
		foreach (Renderer stroke in built.Strokes)
		{
			if (Object.op_Implicit((Object)(object)stroke))
			{
				Look.Colour(stroke, DrawCol, emission);
			}
		}
		foreach (Renderer halo in built.Halos)
		{
			if (Object.op_Implicit((Object)(object)halo))
			{
				Look.HaloColour(halo, emission);
			}
		}
		if (Object.op_Implicit((Object)(object)built.Light))
		{
			RuneLight.Tint(built.Light.GetComponent<Light>(), emission);
		}
		return true;
	}

	private void Update()
	{
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		if (ghost)
		{
			Preview();
		}
		else
		{
			if (!HasData)
			{
				return;
			}
			if (job != null)
			{
				if (!job.Ready || !Trace.TakeFinishSlot())
				{
					return;
				}
				Trace.Job j = job;
				job = null;
				Apply(j);
			}
			if (Etched && built != null && Time.time >= nextSun)
			{
				nextSun = Time.time + 2f;
				if (UpdateDim())
				{
					Retint();
				}
			}
			if (!(Time.time < nextCheck) && built != null)
			{
				nextCheck = Time.time + 2f;
				if (!Trace.HostIds(Frame.Center, Size).SetEquals(built.Hosts))
				{
					Rebuild();
				}
			}
		}
	}

	private void Preview()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_007a: 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_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_020e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0210: Unknown result type (might be due to invalid IL or missing references)
		//IL_0216: Unknown result type (might be due to invalid IL or missing references)
		//IL_021b: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_027b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0285: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0300: Unknown result type (might be due to invalid IL or missing references)
		//IL_0358: Unknown result type (might be due to invalid IL or missing references)
		//IL_035a: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_03de: Expected O, but got Unknown
		//IL_0415: Unknown result type (might be due to invalid IL or missing references)
		//IL_042c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0436: Unknown result type (might be due to invalid IL or missing references)
		//IL_0440: Unknown result type (might be due to invalid IL or missing references)
		//IL_0445: Unknown result type (might be due to invalid IL or missing references)
		//IL_0485: Unknown result type (might be due to invalid IL or missing references)
		Camera mainCamera = Utils.GetMainCamera();
		if (!Object.op_Implicit((Object)(object)mainCamera))
		{
			return;
		}
		Vector3 position = ((Component)mainCamera).transform.position;
		Vector3 forward = ((Component)mainCamera).transform.forward;
		Player localPlayer = Player.m_localPlayer;
		float num = (Object.op_Implicit((Object)(object)localPlayer) ? Mathf.Repeat((float)PlaceRotation.Invoke(localPlayer) * PlaceRotationDegrees.Invoke(localPlayer), 360f) : 0f);
		Vector3 val = position - lastEye;
		bool flag = ((Vector3)(ref val)).sqrMagnitude >= 0.0001f || Vector3.Angle(forward, lastDir) > 0.05f || num != lastTurn || Settings.Version != lastSettings;
		if (Time.time - previewTime < Mathf.Max(flag ? 0.1f : 0.5f, previewCost * 4f))
		{
			return;
		}
		lastEye = position;
		lastDir = forward;
		lastTurn = num;
		lastSettings = Settings.Version;
		previewTime = Time.time;
		float size = Settings.Size;
		Color col = Settings.Colour(Etched);
		Color kept = Settings.KeptFor(Etched);
		Color val2 = Settings.GlowFor(Etched);
		preview?.Clear();
		bool num2 = Trace.Aim(position, forward, 50f, size, ((Component)mainCamera).transform.right, num, out aim);
		float num3 = ((num2 && Object.op_Implicit((Object)(object)localPlayer)) ? Vector3.Distance(((Character)localPlayer).GetEyePoint(), aim.Center) : 0f);
		aimFar = num2 && Object.op_Implicit((Object)(object)localPlayer) && num3 > localPlayer.m_maxPlaceDistance;
		aimValid = num2 && !aimFar;
		aimTime = Time.time;
		aimRune = Rune;
		aimEtched = Etched;
		aimSize = size;
		aimCol = col;
		aimGlow = kept;
		aimLit = Etched && Settings.Light;
		if (!aimValid)
		{
			Grass.Clear(this);
			ghostGrassAt = null;
			if (Object.op_Implicit((Object)(object)previewLight))
			{
				((Behaviour)previewLight).enabled = false;
			}
			return;
		}
		if (ghostGrassAt.HasValue && Vector3.Distance(ghostGrassAt.Value, aim.Center) > 0.25f)
		{
			Grass.Clear(this);
			ghostGrassAt = null;
		}
		aimLayer = LayerAt(aim.Center, size, null);
		if (Etched)
		{
			float num4 = SunDim.Factor(kept, Settings.Glow, SunDim.Day(), SunDim.Sun(aim, size), GlowCap.Bloom);
			val2 *= num4;
			val2.a = 1f;
		}
		Stopwatch stopwatch = Stopwatch.StartNew();
		preview = Trace.Build(aim, size, 0.004f + 0.002f * (float)aimLayer, Look.Material(Rune, Etched), Etched ? Look.HaloFor(Rune) : null, col, val2, preview: true, "preview");
		previewCost = (float)stopwatch.Elapsed.TotalSeconds;
		bool flag2 = aimLit && preview.Sheets > 0 && ((Color)(ref val2)).maxColorComponent > 0f && !Loaded.Any((Paint p) => Object.op_Implicit((Object)(object)p) && p.HasData && Repeats(p, Rune, Etched, aim, size, aimCol, kept, lit: true, share: true));
		if (flag2 && !Object.op_Implicit((Object)(object)previewLight))
		{
			GameObject val3 = new GameObject("PotatoPainter preview light");
			previewLight = val3.AddComponent<Light>();
		}
		if (Object.op_Implicit((Object)(object)previewLight))
		{
			((Behaviour)previewLight).enabled = flag2;
			if (flag2)
			{
				RuneLight.Setup(previewLight, size, val2);
				((Component)previewLight).transform.position = aim.Center + aim.Normal * 0.15f;
			}
		}
		if (!flag && !ghostGrassAt.HasValue && preview.OnGround)
		{
			Grass.Set(this, on: true, aim, size);
			ghostGrassAt = aim.Center;
		}
	}

	private static int LayerAt(Vector3 center, float size, HashSet<Paint> leaveOut)
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		int num = 0;
		foreach (Paint item in Loaded)
		{
			if (Object.op_Implicit((Object)(object)item) && item.HasData && (leaveOut == null || !leaveOut.Contains(item)) && Vector3.Distance(item.Frame.Center, center) < (item.Size + size) * 0.5f * 0.9f)
			{
				num = Mathf.Max(num, item.Layer + 1);
			}
		}
		return num;
	}

	private void OnDisable()
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		if (ghost)
		{
			aimValid = false;
			preview?.Clear();
			preview = null;
			if (Object.op_Implicit((Object)(object)previewLight))
			{
				((Behaviour)previewLight).enabled = false;
			}
			Grass.Clear(this);
			ghostGrassAt = null;
			lastEye = Vector3.positiveInfinity;
		}
	}

	private void OnDestroy()
	{
		job = null;
		if (listed)
		{
			Loaded.Remove(this);
		}
		Grass.Clear(this);
		built?.Clear();
		preview?.Clear();
		if (Object.op_Implicit((Object)(object)previewLight))
		{
			Object.Destroy((Object)(object)((Component)previewLight).gameObject);
		}
	}

	internal void Delete()
	{
		if (Object.op_Implicit((Object)(object)nview) && nview.IsValid())
		{
			nview.ClaimOwnership();
			ZNetScene.instance.Destroy(((Component)this).gameObject);
		}
	}
}
internal static class Settings
{
	internal const float SizeMin = 0.25f;

	internal const float SizeMax = 2f;

	internal const float GlowMin = 0.25f;

	internal const float GlowMax = 1f;

	internal const float EtchedSurface = 0.62f;

	private const string SaveKey = "PotatoPainter.Panel";

	internal static float PaintH;

	internal static float PaintS;

	internal static float PaintV;

	internal static float EtchH;

	internal static float EtchS;

	internal static float Glow;

	internal static float Size;

	internal static bool Light;

	internal static int Version;

	internal static Color PaintColor => Opaque(Color.HSVToRGB(PaintH, PaintS, PaintV));

	internal static Color GlowColor => Opaque(Color.HSVToRGB(EtchH, EtchS, 1f));

	internal static Color EtchColor => Opaque(GlowColor * 0.62f);

	internal static Color KeptGlow => Opaque(GlowColor * Glow);

	internal static Color Emission => GlowCap.Emission(KeptGlow, share: true);

	static Settings()
	{
		Light = true;
		Defaults(painted: true, etched: true);
	}

	internal static void Defaults(bool painted, bool etched)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (mig