Decompiled source of Lyngbakr v1.0.5

Lyngbakr.dll

Decompiled 5 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Splatform;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = "")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Lyngbakr
{
	internal static class DeckAnimals
	{
		[HarmonyPatch(typeof(ZNetView), "Awake")]
		private static class Stragglers
		{
			private static void Postfix(ZNetView __instance)
			{
				//IL_0053: Unknown result type (might be due to invalid IL or missing references)
				ZDO zDO = __instance.GetZDO();
				if (zDO != null && zDO.GetBool("lyng_deck_animal", false) && zDO.IsOwner() && !zDO.GetBool(ZDOVars.s_tamed, false))
				{
					LyngbakrBody instance = LyngbakrBody.Instance;
					if (!((Object)(object)instance == (Object)null) && !((Object)(object)instance.PieceRoot == (Object)null) && !instance.NearFootprint(((Component)__instance).transform.position, 10f))
					{
						__instance.Destroy();
					}
				}
			}
		}

		internal const string KeyDeckAnimal = "lyng_deck_animal";

		private const float Interval = 20f;

		private const int MaxOnDeck = 8;

		private const float MinPlayerDistance = 20f;

		private static float _timer = 20f;

		private static List<SpawnData> _peaceful;

		private static List<SpawnData> Peaceful()
		{
			if (_peaceful != null)
			{
				return _peaceful;
			}
			GameObject val = (((Object)(object)ZoneSystem.instance != (Object)null) ? ZoneSystem.instance.m_zoneCtrlPrefab : null);
			SpawnSystem val2 = (((Object)(object)val != (Object)null) ? val.GetComponent<SpawnSystem>() : null);
			if ((Object)(object)val2 == (Object)null)
			{
				return null;
			}
			_peaceful = (from s in val2.m_spawnLists.SelectMany((SpawnSystemList l) => l.m_spawners)
				where s.m_enabled && (Object)(object)s.m_prefab != (Object)null && IsPeaceful(s)
				select s).ToList();
			LyngbakrPlugin.Log.LogInfo((object)("Peaceful animals for his back: " + ((_peaceful.Count == 0) ? "none" : string.Join(", ", _peaceful.Select((SpawnData s) => $"{((Object)s.m_prefab).name} ({s.m_biome})").Distinct()))));
			return _peaceful;
		}

		private static bool IsPeaceful(SpawnData s)
		{
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Invalid comparison between Unknown and I4
			Character component = s.m_prefab.GetComponent<Character>();
			BaseAI component2 = s.m_prefab.GetComponent<BaseAI>();
			if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null || component.IsBoss() || component.m_flying || component2.m_randomFly || component2.m_serpentMovement)
			{
				return false;
			}
			if (s.m_minOceanDepth != s.m_maxOceanDepth && s.m_minOceanDepth > 0f)
			{
				return false;
			}
			if (s.m_huntPlayer)
			{
				return false;
			}
			if (!(component2 is AnimalAI) && (int)component.m_faction != 1)
			{
				return component2.m_passiveAggresive;
			}
			return true;
		}

		internal static void Tick(LyngbakrBody body)
		{
			_timer -= 1f;
			if (_timer > 0f)
			{
				return;
			}
			_timer = 20f;
			DeckTerrain instance = DeckTerrain.Instance;
			List<SpawnData> list = Peaceful();
			if ((Object)(object)instance == (Object)null || list == null || list.Count == 0 || (Object)(object)ZNetScene.instance == (Object)null)
			{
				return;
			}
			try
			{
				TrySpawn(body, instance, list);
			}
			catch (Exception arg)
			{
				LyngbakrPlugin.Log.LogError((object)$"Deck animal spawn failed: {arg}");
			}
		}

		private static void TrySpawn(LyngbakrBody body, DeckTerrain deck, List<SpawnData> list)
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f3: 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_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_017b: 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_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			List<Character> list2 = Character.GetAllCharacters().Where(delegate(Character ch)
			{
				//IL_0049: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)ch != (Object)null && !ch.IsPlayer())
				{
					ZNetView component2 = ((Component)ch).GetComponent<ZNetView>();
					if (((component2 != null) ? component2.GetZDO() : null) != null && ((Component)ch).GetComponent<ZNetView>().GetZDO().GetBool("lyng_deck_animal", false))
					{
						return body.NearFootprint(((Component)ch).transform.position, 10f);
					}
				}
				return false;
			}).ToList();
			if (list2.Count >= 8 || !deck.RandomGroundPoint(out var point, out var normal))
			{
				return;
			}
			Biome biome = deck.BiomeAt(point);
			List<SpawnData> list3 = list.Where((SpawnData s) => (s.m_biome & biome) != 0 && Allowed(s, point, normal)).ToList();
			if (list3.Count == 0)
			{
				return;
			}
			SpawnData spawn = list3[Random.Range(0, list3.Count)];
			if (Random.Range(0f, 100f) > spawn.m_spawnChance || list2.Count((Character ch) => Utils.GetPrefabName(((Component)ch).gameObject) == ((Object)spawn.m_prefab).name) >= Mathf.Max(1, spawn.m_maxSpawned))
			{
				return;
			}
			int num = Random.Range(spawn.m_groupSizeMin, spawn.m_groupSizeMax + 1);
			num = Mathf.Clamp(num, 1, 8 - list2.Count);
			for (int num2 = 0; num2 < num; num2++)
			{
				Vector3 point2 = point;
				if (num2 > 0)
				{
					Vector2 val = Random.insideUnitCircle * spawn.m_groupRadius;
					if (!deck.GroundAt(point + new Vector3(val.x, 0f, val.y), out point2, out var _))
					{
						continue;
					}
				}
				ZNetView component = Object.Instantiate<GameObject>(spawn.m_prefab, point2 + Vector3.up * 0.5f, Quaternion.Euler(0f, Random.Range(0f, 360f), 0f)).GetComponent<ZNetView>();
				if (component != null)
				{
					ZDO zDO = component.GetZDO();
					if (zDO != null)
					{
						zDO.Set("lyng_deck_animal", true);
					}
				}
			}
			LyngbakrPlugin.Log.LogInfo((object)$"{num} {((Object)spawn.m_prefab).name} wandered onto his back ({biome})");
		}

		private static bool Allowed(SpawnData s, Vector3 p, Vector3 normal)
		{
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			if (!string.IsNullOrEmpty(s.m_requiredGlobalKey) && !ZoneSystem.instance.GetGlobalKey(s.m_requiredGlobalKey))
			{
				return false;
			}
			if (s.m_requiredEnvironments.Count > 0 && !EnvMan.instance.IsEnvironment(s.m_requiredEnvironments))
			{
				return false;
			}
			if ((!s.m_spawnAtDay && EnvMan.IsDay()) || (!s.m_spawnAtNight && EnvMan.IsNight()))
			{
				return false;
			}
			if (normal.y < Mathf.Cos((float)Math.PI / 180f * s.m_maxTilt))
			{
				return false;
			}
			if (Player.IsPlayerInRange(p, 20f))
			{
				return false;
			}
			if (!s.m_insidePlayerBase && Object.op_Implicit((Object)(object)EffectArea.IsPointInsideArea(p, (Type)4, 0f)))
			{
				return false;
			}
			return true;
		}
	}
	internal class DeckTerrain : MonoBehaviour
	{
		private class GrassKind
		{
			public Mesh Mesh;

			public Material Material;

			public ShadowCastingMode Shadows;
		}

		private class GrassChunk
		{
			public Vector3 Centre;

			public readonly List<Matrix4x4>[] Local;

			public GrassChunk(int kinds)
			{
				Local = new List<Matrix4x4>[kinds];
				for (int i = 0; i < kinds; i++)
				{
					Local[i] = new List<Matrix4x4>();
				}
			}
		}

		[HarmonyPatch(typeof(ClutterSystem), "GetGroundInfo")]
		private static class ClutterGroundInfo
		{
			private static void Postfix(ref Heightmap hmap, ref bool __result)
			{
				if (__result && (Object)(object)Instance != (Object)null && (Object)(object)hmap != (Object)null && (Object)(object)hmap == (Object)(object)Instance.Hmap)
				{
					__result = false;
				}
			}
		}

		internal class NoHover : MonoBehaviour, Hoverable
		{
			public string GetHoverText()
			{
				return "";
			}

			public string GetHoverName()
			{
				return "";
			}

			public float GetHoverOffset()
			{
				return 0f;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "Generate")]
		private static class Generate_Patch
		{
			private static bool Prefix(Heightmap __instance, ref bool __result)
			{
				if (!Is(__instance))
				{
					return true;
				}
				__result = Instance.Generate();
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "WorldToVertex")]
		private static class WorldToVertex_Patch
		{
			private static bool Prefix(Heightmap __instance, Vector3 worldPos, ref int x, ref int y)
			{
				//IL_0019: Unknown result type (might be due to invalid IL or missing references)
				//IL_001a: Unknown result type (might be due to invalid IL or missing references)
				if (!Is(__instance))
				{
					return true;
				}
				Instance.ToVertex(Instance.T.InverseTransformPoint(worldPos), out x, out y);
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "WorldToVertexMask")]
		private static class WorldToVertexMask_Patch
		{
			private static bool Prefix(Heightmap __instance, Vector3 worldPos, ref int x, ref int y)
			{
				//IL_0019: Unknown result type (might be due to invalid IL or missing references)
				//IL_001a: Unknown result type (might be due to invalid IL or missing references)
				if (!Is(__instance))
				{
					return true;
				}
				Instance.ToVertexMask(Instance.T.InverseTransformPoint(worldPos), out x, out y);
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "VertexMaskToWorld")]
		private static class VertexMaskToWorld_Patch
		{
			private static bool Prefix(Heightmap __instance, int x, int y, ref Vector3 __result)
			{
				//IL_0047: Unknown result type (might be due to invalid IL or missing references)
				//IL_004c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0051: Unknown result type (might be due to invalid IL or missing references)
				if (!Is(__instance))
				{
					return true;
				}
				int num = Instance.W / 2;
				__result = Instance.T.TransformPoint(new Vector3(((float)(x - num) - 0.5f) * 1f, 0f, ((float)(y - num) - 0.5f) * 1f));
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "WorldToNormalizedHM")]
		private static class WorldToNormalizedHM_Patch
		{
			private static bool Prefix(Heightmap __instance, Vector3 worldPos, ref float x, ref float y)
			{
				//IL_0014: Unknown result type (might be due to invalid IL or missing references)
				//IL_0015: Unknown result type (might be due to invalid IL or missing references)
				//IL_001a: Unknown result type (might be due to invalid IL or missing references)
				//IL_002e: 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)
				if (!Is(__instance))
				{
					return true;
				}
				Vector3 val = Instance.T.InverseTransformPoint(worldPos);
				float num = (float)Instance.W * 1f;
				x = val.x / num + 0.5f;
				y = val.z / num + 0.5f;
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "GetWorldHeight")]
		private static class GetWorldHeight_Patch
		{
			private static bool Prefix(Heightmap __instance, Vector3 worldPos, ref float height, ref bool __result)
			{
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				if (!Is(__instance))
				{
					return true;
				}
				__result = WorldHeight(null, worldPos, out height);
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "GetWorldBaseHeight")]
		private static class GetWorldBaseHeight_Patch
		{
			private static bool Prefix(Heightmap __instance, Vector3 worldPos, ref float height, ref bool __result)
			{
				//IL_0015: Unknown result type (might be due to invalid IL or missing references)
				if (!Is(__instance))
				{
					return true;
				}
				__result = WorldHeight(Instance._digFloorPlus8, worldPos, out height);
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "UpdateCornerDepths")]
		private static class UpdateCornerDepths_Patch
		{
			private static void Postfix(Heightmap __instance)
			{
				if (Is(__instance))
				{
					float[] array = HDepth.Invoke(__instance);
					for (int i = 0; i < array.Length; i++)
					{
						array[i] = 0f;
					}
					Material obj = HMatInstance.Invoke(__instance);
					if (obj != null)
					{
						obj.SetFloatArray("_depth", array);
					}
				}
			}
		}

		[HarmonyPatch(typeof(Heightmap), "GetBiome")]
		private static class GetBiome_Patch
		{
			private static bool Prefix(Heightmap __instance, Vector3 point, ref Biome __result)
			{
				//IL_0010: Unknown result type (might be due to invalid IL or missing references)
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				//IL_0017: Expected I4, but got Unknown
				if (!Is(__instance))
				{
					return true;
				}
				__result = (Biome)(int)Instance.BiomeAt(point);
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "RebuildRenderMesh")]
		private static class RebuildRenderMesh_Patch
		{
			private static void Postfix(Heightmap __instance)
			{
				//IL_007b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0080: Unknown result type (might be due to invalid IL or missing references)
				//IL_0087: 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_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_00a9: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
				//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
				//IL_011e: Unknown result type (might be due to invalid IL or missing references)
				if (!Is(__instance))
				{
					return;
				}
				Mesh val = HRenderMesh.Invoke(__instance);
				DeckTerrain instance = Instance;
				if ((Object)(object)val == (Object)null || val.vertexCount != instance.N * instance.N)
				{
					return;
				}
				Color[] array = (Color[])(object)new Color[val.vertexCount];
				Color[] array2 = DeckLook.Biomes.Select((Biome b) => Color32.op_Implicit(Heightmap.GetBiomeColor(b))).ToArray();
				for (int num = 0; num < array.Length; num++)
				{
					Color val2 = Color.clear;
					for (int num2 = 0; num2 < 6; num2++)
					{
						val2 += array2[num2] * instance._weights[num * 6 + num2];
					}
					array[num] = val2;
				}
				val.colors = array;
				List<Vector4> list = new List<Vector4>(val.vertexCount);
				for (int num3 = 0; num3 < val.vertexCount; num3++)
				{
					list.Add(new Vector4((instance._inside[num3] || instance._edgeDist[num3] < 4f) ? 1f : 0f, 0f, 0f, 0f));
				}
				val.SetUVs(3, list);
			}
		}

		[HarmonyPatch(typeof(Heightmap), "FindHeightmap", new Type[] { typeof(Vector3) })]
		private static class FindHeightmap_Patch
		{
			private static bool Prefix(Vector3 point, ref Heightmap __result)
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)Instance == (Object)null || !Instance.Contains(point))
				{
					return true;
				}
				__result = Instance.Hmap;
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "FindHeightmap", new Type[]
		{
			typeof(Vector3),
			typeof(float),
			typeof(List<Heightmap>)
		})]
		private static class FindHeightmapList_Patch
		{
			private static bool Prefix(Vector3 point, float radius, List<Heightmap> heightmaps)
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)Instance == (Object)null || !Instance.Contains(point, radius))
				{
					return true;
				}
				heightmaps.Add(Instance.Hmap);
				return false;
			}
		}

		[HarmonyPatch(typeof(Heightmap), "FindBiomeClutter")]
		private static class FindBiomeClutter_Patch
		{
			private static bool Prefix(Vector3 point, ref Biome __result)
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)Instance == (Object)null || !Instance.Contains(point, 2f))
				{
					return true;
				}
				__result = (Biome)0;
				return false;
			}
		}

		[HarmonyPatch(typeof(TerrainOp), "Awake")]
		private static class TerrainOp_Awake
		{
			private static bool Prefix(TerrainOp __instance)
			{
				//IL_001f: Unknown result type (might be due to invalid IL or missing references)
				if (TerrainOp.m_forceDisableTerrainOps || (Object)(object)Instance == (Object)null || !Instance.Contains(((Component)__instance).transform.position))
				{
					return true;
				}
				try
				{
					Instance.RequestOp(__instance);
				}
				catch (Exception arg)
				{
					LyngbakrPlugin.Log.LogError((object)$"Deck terrain op failed: {arg}");
				}
				OpOnPlaced(__instance);
				Object.Destroy((Object)(object)((Component)__instance).gameObject);
				return false;
			}
		}

		private static int _groundMask;

		private const float ChunkSize = 16f;

		private List<GrassKind> _grassKinds;

		private List<Clutter> _grassClutter;

		private GrassChunk[] _chunks;

		private int _chunksPerSide;

		private bool _grassDirty = true;

		private float _grassRebuildAt;

		private readonly Matrix4x4[] _drawBuffer = (Matrix4x4[])(object)new Matrix4x4[1023];

		private static int _grassLayer = -1;

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

		private bool _allChunksDirty = true;

		private float[] _grassHeights;

		private Color[] _grassMask;

		private const string KeyScenery = "lyng_scenery";

		internal const string KeySceneryObj = "lyng_scenery_obj";

		private const int SceneryVersion = 5;

		private bool _populating;

		internal static bool Scouting;

		internal const string KeyPlayerMade = "lyng_player";

		internal static DeckTerrain Instance;

		internal const string KeyDeck = "lyng_deck";

		private const string RpcOp = "LyngbakrDeckOp";

		private const float S = 1f;

		internal Heightmap Hmap;

		private Transform T;

		private ZNetView _nview;

		private int W;

		private int N;

		private float[] _base;

		private bool[] _inside;

		private float[] _maxDig;

		private float[] _maxRaise;

		private const float MaxEdit = 8f;

		private const float RimRamp = 12f;

		private float[] _digFloorPlus8;

		private Color[] _baseMask;

		private Color[] _mask;

		private float[] _level;

		private float[] _smooth;

		private bool[] _modH;

		private bool[] _modP;

		private Color[] _paint;

		private uint _loadedRevision = uint.MaxValue;

		private float _pollTimer;

		private BiomeSector _sector;

		private Biome _biome;

		private static readonly FieldRef<Heightmap, List<float>> HHeights = AccessTools.FieldRefAccess<Heightmap, List<float>>("m_heights");

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

		private static readonly FieldRef<Heightmap, HMBuildData> HBuild = AccessTools.FieldRefAccess<Heightmap, HMBuildData>("m_buildData");

		private static readonly FieldRef<Heightmap, BiomeSector[]> HCorners = AccessTools.FieldRefAccess<Heightmap, BiomeSector[]>("m_cornerBiomes");

		private static readonly FieldRef<Heightmap, float[]> HDepth = AccessTools.FieldRefAccess<Heightmap, float[]>("m_oceanDepth");

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

		private static readonly FieldRef<List<Heightmap>> AllHeightmaps = AccessTools.StaticFieldRefAccess<List<Heightmap>>(AccessTools.Field(typeof(Heightmap), "s_heightmaps"));

		private static readonly Action<Heightmap> HInitialize = AccessTools.MethodDelegate<Action<Heightmap>>(AccessTools.Method(typeof(Heightmap), "Initialize", (Type[])null, (Type[])null), (object)null, true);

		private static readonly Action<TerrainOp> OpOnPlaced = AccessTools.MethodDelegate<Action<TerrainOp>>(AccessTools.Method(typeof(TerrainOp), "OnPlaced", (Type[])null, (Type[])null), (object)null, true);

		internal readonly Dictionary<Biome, float> BiomeShare = new Dictionary<Biome, float>();

		private int _seed;

		private string _biomeChoices;

		private Collider _shell;

		private bool _patchwork;

		private float[] _weights;

		private byte[] _biomeAt;

		private float[] _edgeDist;

		private float[] _srcAltitude;

		private Vector2 _src;

		private readonly List<Biome> _regions = new List<Biome>();

		private const int NB = 6;

		private const float WaterLevel = 30f;

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

		private static int GroundMask
		{
			get
			{
				if (_groundMask == 0)
				{
					return _groundMask = LayerMask.GetMask(new string[5] { "Default", "static_solid", "Default_small", "piece", "terrain" });
				}
				return _groundMask;
			}
		}

		internal int Signature => ScenerySignature();

		internal string BuiltKey { get; private set; }

		internal bool RandomGroundPoint(out Vector3 point, out Vector3 normal)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			point = (normal = Vector3.zero);
			if ((Object)(object)Hmap == (Object)null || (Object)(object)T == (Object)null)
			{
				return false;
			}
			float num = (float)W * 1f * 0.5f;
			for (int i = 0; i < 10; i++)
			{
				Vector3 world = T.TransformPoint(new Vector3(Random.Range(0f - num, num), 0f, Random.Range(0f - num, num)));
				if (GroundAt(world, out point, out normal))
				{
					return true;
				}
			}
			return false;
		}

		internal bool GroundAt(Vector3 world, out Vector3 point, out Vector3 normal)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: 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_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: 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)
			point = (normal = Vector3.zero);
			RaycastHit val = default(RaycastHit);
			if (!Physics.Raycast(new Vector3(world.x, T.position.y + 80f, world.z), Vector3.down, ref val, 200f, GroundMask))
			{
				return false;
			}
			if ((Object)(object)((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Heightmap>() != (Object)(object)Hmap)
			{
				return false;
			}
			if (!Contains(((RaycastHit)(ref val)).point))
			{
				return false;
			}
			point = ((RaycastHit)(ref val)).point;
			normal = ((RaycastHit)(ref val)).normal;
			return true;
		}

		private void SetupGrass()
		{
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			ClutterSystem instance = ClutterSystem.instance;
			_grassKinds = new List<GrassKind>();
			_grassClutter = new List<Clutter>();
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			if (_grassLayer < 0)
			{
				FieldInfo fieldInfo = AccessTools.Field(typeof(InstanceRenderer), "s_layer");
				_grassLayer = ((fieldInfo != null) ? ((int)fieldInfo.GetValue(null)) : 0);
			}
			foreach (Clutter item in instance.m_clutter)
			{
				if (item.m_enabled && item.m_instanced && !((Object)(object)item.m_prefab == (Object)null))
				{
					InstanceRenderer componentInChildren = item.m_prefab.GetComponentInChildren<InstanceRenderer>(true);
					if (!((Object)(object)componentInChildren == (Object)null) && !((Object)(object)componentInChildren.m_mesh == (Object)null) && !((Object)(object)componentInChildren.m_material == (Object)null))
					{
						_grassKinds.Add(new GrassKind
						{
							Mesh = componentInChildren.m_mesh,
							Material = componentInChildren.m_material,
							Shadows = componentInChildren.m_shadowCasting
						});
						_grassClutter.Add(item);
					}
				}
			}
			_chunksPerSide = Mathf.CeilToInt((float)W * 1f / 16f);
			_grassDirty = true;
		}

		private static float GrassQuality()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected I4, but got Unknown
			ClutterSystem instance = ClutterSystem.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return 0f;
			}
			Quality value = Traverse.Create((object)instance).Field("m_quality").GetValue<Quality>();
			return (int)value switch
			{
				0 => 0f, 
				1 => 0.25f, 
				2 => 0.5f, 
				_ => 1f, 
			};
		}

		private bool SampleDeck(float lx, float lz, out float h, out Vector3 normal, out int nearest)
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			List<float> list = HHeights.Invoke(Hmap);
			float num = lx / 1f + (float)W / 2f;
			float num2 = lz / 1f + (float)W / 2f;
			int num3 = Mathf.FloorToInt(num);
			int num4 = Mathf.FloorToInt(num2);
			h = 0f;
			normal = Vector3.up;
			nearest = 0;
			if (num3 < 0 || num4 < 0 || num3 >= W || num4 >= W)
			{
				return false;
			}
			float num5 = num - (float)num3;
			float num6 = num2 - (float)num4;
			float num7 = list[num4 * N + num3];
			float num8 = list[num4 * N + num3 + 1];
			float num9 = list[(num4 + 1) * N + num3];
			float num10 = list[(num4 + 1) * N + num3 + 1];
			h = Mathf.Lerp(Mathf.Lerp(num7, num8, num5), Mathf.Lerp(num9, num10, num5), num6);
			Vector3 val = new Vector3((0f - Mathf.Lerp(num8 - num7, num10 - num9, num6)) / 1f, 1f, (0f - Mathf.Lerp(num9 - num7, num10 - num8, num5)) / 1f);
			normal = ((Vector3)(ref val)).normalized;
			nearest = Mathf.RoundToInt(num2) * N + Mathf.RoundToInt(num);
			return true;
		}

		private void RebuildGrass()
		{
			//IL_00aa: 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)
			_grassDirty = false;
			if (_grassKinds == null)
			{
				SetupGrass();
			}
			int count = _grassKinds.Count;
			if (_chunks == null || _chunks.Length != _chunksPerSide * _chunksPerSide)
			{
				_chunks = new GrassChunk[_chunksPerSide * _chunksPerSide];
				_allChunksDirty = true;
			}
			List<float> list = HHeights.Invoke(Hmap);
			if (!_allChunksDirty && _grassHeights != null)
			{
				for (int i = 0; i < _base.Length; i++)
				{
					if (_grassHeights[i] != list[i] || _grassMask[i] != _mask[i])
					{
						int num = Mathf.Clamp((int)((float)(i % N) * 1f / 16f), 0, _chunksPerSide - 1);
						int num2 = Mathf.Clamp((int)((float)(i / N) * 1f / 16f), 0, _chunksPerSide - 1);
						_dirtyChunks.Add(num2 * _chunksPerSide + num);
					}
				}
			}
			_grassHeights = list.ToArray();
			_grassMask = (Color[])_mask.Clone();
			int total = 0;
			int num3 = 0;
			for (int j = 0; j < _chunks.Length; j++)
			{
				if (_allChunksDirty || _dirtyChunks.Contains(j))
				{
					_chunks[j] = BuildChunk(j % _chunksPerSide, j / _chunksPerSide, count, ref total);
					num3++;
				}
			}
			_dirtyChunks.Clear();
			if (_allChunksDirty)
			{
				LyngbakrPlugin.Log.LogInfo((object)$"Deck grass: {total} plants of {count} kinds");
			}
			_allChunksDirty = false;
		}

		private GrassChunk BuildChunk(int cx, int cz, int kinds, ref int total)
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: 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_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: 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_015f: 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_0246: Unknown result type (might be due to invalid IL or missing references)
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_026e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_0289: 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_0299: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0337: Unknown result type (might be due to invalid IL or missing references)
			//IL_033c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0340: Unknown result type (might be due to invalid IL or missing references)
			//IL_034c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0361: Unknown result type (might be due to invalid IL or missing references)
			//IL_0394: Unknown result type (might be due to invalid IL or missing references)
			//IL_039b: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0411: Unknown result type (might be due to invalid IL or missing references)
			//IL_0416: Unknown result type (might be due to invalid IL or missing references)
			//IL_0418: Unknown result type (might be due to invalid IL or missing references)
			//IL_0429: Unknown result type (might be due to invalid IL or missing references)
			//IL_042e: Unknown result type (might be due to invalid IL or missing references)
			//IL_040a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0433: Unknown result type (might be due to invalid IL or missing references)
			//IL_045b: 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_0464: Unknown result type (might be due to invalid IL or missing references)
			//IL_046b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0470: Unknown result type (might be due to invalid IL or missing references)
			float num = (float)(-W) * 1f / 2f;
			GrassChunk grassChunk = new GrassChunk(kinds)
			{
				Centre = new Vector3(num + ((float)cx + 0.5f) * 16f, 0f, num + ((float)cz + 0.5f) * 16f)
			};
			float num2 = GrassQuality();
			ClutterSystem instance = ClutterSystem.instance;
			if (kinds == 0 || num2 <= 0f || (Object)(object)instance == (Object)null)
			{
				return grassChunk;
			}
			Biome val = (Biome)0;
			for (int i = 0; i <= 16; i += 2)
			{
				for (int j = 0; j <= 16; j += 2)
				{
					int num3 = Mathf.Min(N - 1, (int)((float)cx * 16f / 1f) + j);
					int num4 = Mathf.Min(N - 1, (int)((float)cz * 16f / 1f) + i) * N + num3;
					if (_inside[num4])
					{
						val |= DeckLook.Biomes[_biomeAt[num4]];
					}
				}
			}
			if ((int)val == 0)
			{
				return grassChunk;
			}
			float num5 = Mathf.Max(1f, instance.m_grassPatchSize);
			Random random = new Random(_seed * 92821 + cz * 7919 + cx * 104729 + 3);
			Vector3 val5 = default(Vector3);
			for (int k = 0; k < kinds; k++)
			{
				Clutter val2 = _grassClutter[k];
				if ((val2.m_biome & val) == 0)
				{
					continue;
				}
				int num6 = Mathf.RoundToInt((float)val2.m_amount * instance.m_amountScale * num2 * 256f / (num5 * num5));
				float num7 = Mathf.Cos(val2.m_maxTilt * ((float)Math.PI / 180f));
				float num8 = Mathf.Cos(val2.m_minTilt * ((float)Math.PI / 180f));
				InstanceRenderer componentInChildren = val2.m_prefab.GetComponentInChildren<InstanceRenderer>(true);
				for (int l = 0; l < num6; l++)
				{
					float num9 = num + ((float)cx + (float)random.NextDouble()) * 16f;
					float num10 = num + ((float)cz + (float)random.NextDouble()) * 16f;
					float num11 = (float)random.NextDouble() * 360f;
					float num12 = (float)random.NextDouble();
					if (!SampleDeck(num9, num10, out var h, out var normal, out var nearest) || !_inside[nearest] || _edgeDist[nearest] < 1f || (val2.m_biome & DeckLook.Biomes[_biomeAt[nearest]]) == 0 || normal.y > num8 || normal.y < num7)
					{
						continue;
					}
					Color val3 = _mask[nearest];
					if (((val3.r > 0.5f || val3.g > 0.5f || val3.b > 0.5f) ? (!val2.m_onCleared) : (!val2.m_onUncleared)) || (val2.m_minVegetation != val2.m_maxVegetation && (val3.a < val2.m_minVegetation || val3.a > val2.m_maxVegetation)))
					{
						continue;
					}
					float num13 = _srcAltitude[nearest];
					if (num13 < val2.m_minAlt || num13 > val2.m_maxAlt)
					{
						continue;
					}
					Vector2 val4 = SourceOf(num9, num10);
					((Vector3)(ref val5))..ctor(val4.x, 0f, val4.y);
					if (val2.m_inForest)
					{
						float forestFactor = WorldGenerator.GetForestFactor(val5);
						if (forestFactor < val2.m_forestTresholdMin || forestFactor > val2.m_forestTresholdMax)
						{
							continue;
						}
					}
					if (val2.m_fractalScale > 0f)
					{
						float num14 = Utils.Fbm(val5 * 0.01f * val2.m_fractalScale + new Vector3(val2.m_fractalOffset, 0f, 0f), 3, 1.6f, 0.7f);
						if (num14 < val2.m_fractalTresholdMin || num14 > val2.m_fractalTresholdMax)
						{
							continue;
						}
					}
					Quaternion val6 = (val2.m_terrainTilt ? (Quaternion.FromToRotation(Vector3.up, normal) * Quaternion.Euler(0f, num11, 0f)) : Quaternion.Euler(0f, num11, 0f));
					float num15 = Mathf.Lerp(val2.m_scaleMin, val2.m_scaleMax, num12);
					grassChunk.Local[k].Add(Matrix4x4.TRS(new Vector3(num9, h, num10), val6, componentInChildren.m_scale * num15));
					total++;
				}
			}
			return grassChunk;
		}

		private void LateUpdate()
		{
			//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_00a0: 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_00ac: 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_00ea: 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_00f6: 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_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Hmap == (Object)null || !((Behaviour)Hmap).enabled)
			{
				return;
			}
			if (_grassDirty && Time.time >= _grassRebuildAt)
			{
				RebuildGrass();
			}
			if (_chunks == null || _grassKinds == null || _grassKinds.Count == 0)
			{
				return;
			}
			Camera mainCamera = Utils.GetMainCamera();
			if ((Object)(object)mainCamera == (Object)null)
			{
				return;
			}
			float num = (((Object)(object)ClutterSystem.instance != (Object)null) ? ClutterSystem.instance.m_distance : 60f) + 16f;
			Vector3 val = T.InverseTransformPoint(((Component)mainCamera).transform.position);
			Matrix4x4 localToWorldMatrix = T.localToWorldMatrix;
			GrassChunk[] chunks = _chunks;
			foreach (GrassChunk grassChunk in chunks)
			{
				if (grassChunk == null)
				{
					continue;
				}
				Vector2 val2 = new Vector2(grassChunk.Centre.x - val.x, grassChunk.Centre.z - val.z);
				if (((Vector2)(ref val2)).sqrMagnitude > num * num)
				{
					continue;
				}
				for (int j = 0; j < _grassKinds.Count; j++)
				{
					List<Matrix4x4> list = grassChunk.Local[j];
					if (list.Count == 0)
					{
						continue;
					}
					GrassKind grassKind = _grassKinds[j];
					for (int k = 0; k < list.Count; k += _drawBuffer.Length)
					{
						int num2 = Mathf.Min(_drawBuffer.Length, list.Count - k);
						for (int l = 0; l < num2; l++)
						{
							_drawBuffer[l] = localToWorldMatrix * list[k + l];
						}
						Graphics.DrawMeshInstanced(grassKind.Mesh, 0, grassKind.Material, _drawBuffer, num2, (MaterialPropertyBlock)null, grassKind.Shadows, true, _grassLayer);
					}
				}
			}
		}

		private void GrassGroundChanged()
		{
			_grassDirty = true;
			_grassRebuildAt = Time.time + 0.3f;
		}

		private int ScenerySignature()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected I4, but got Unknown
			return _seed * 31 + 35 + (_patchwork ? 1 : 0) + _biome * 1009 + _biomeChoices.GetHashCode() % 1000;
		}

		private IEnumerator EnsureScenery()
		{
			if (Scouting)
			{
				yield break;
			}
			_populating = true;
			try
			{
				yield return null;
				ZDO zDO = _nview.GetZDO();
				int num = ScenerySignature();
				if (zDO != null && zDO.GetInt("lyng_scenery", 0) != num)
				{
					yield return Populate(num);
				}
			}
			finally
			{
				_populating = false;
			}
		}

		internal void CheckScenery()
		{
			if (!Scouting && !_populating && !((Object)(object)Hmap == (Object)null) && !((Object)(object)_nview == (Object)null) && _nview.IsValid() && _nview.IsOwner() && _nview.GetZDO().GetInt("lyng_scenery", 0) != ScenerySignature())
			{
				((MonoBehaviour)this).StartCoroutine(EnsureScenery());
			}
		}

		internal void RegenerateScenery()
		{
			if (!_nview.IsOwner())
			{
				_nview.ClaimOwnership();
			}
			ZDO zDO = _nview.GetZDO();
			if (zDO != null)
			{
				zDO.Set("lyng_scenery", 0);
			}
			((MonoBehaviour)this).StartCoroutine(EnsureScenery());
		}

		internal void ClearForReseed()
		{
			Transform val = LyngbakrBody.Instance?.PieceRoot;
			if ((Object)(object)val != (Object)null && (Object)(object)ZNetScene.instance != (Object)null)
			{
				int num = 0;
				foreach (ZNetView item in ((Component)val).GetComponentsInChildren<ZNetView>(true).ToList())
				{
					ZDO zDO = item.GetZDO();
					if (zDO == null || (Object)(object)((Component)item).transform == (Object)(object)T)
					{
						continue;
					}
					Piece component = ((Component)item).GetComponent<Piece>();
					if ((!((Object)(object)component != (Object)null) || component.GetCreator() == 0L) && zDO.GetInt("lyng_player", 0) != 1)
					{
						if (!item.IsOwner())
						{
							item.ClaimOwnership();
						}
						ZNetScene.instance.Destroy(((Component)item).gameObject);
						num++;
					}
				}
				LyngbakrPlugin.Log.LogInfo((object)$"Reseed: cleared {num} objects (player builds and plantings kept)");
			}
			Array.Clear(_level, 0, _level.Length);
			Array.Clear(_smooth, 0, _smooth.Length);
			Array.Clear(_modH, 0, _modH.Length);
			Array.Clear(_modP, 0, _modP.Length);
			if (!_nview.IsOwner())
			{
				_nview.ClaimOwnership();
			}
			Save();
			ZDO zDO2 = _nview.GetZDO();
			if (zDO2 != null)
			{
				zDO2.Set("lyng_scenery", 0);
			}
		}

		internal void ClearScenery()
		{
			Transform val = LyngbakrBody.Instance?.PieceRoot;
			if ((Object)(object)val == (Object)null || (Object)(object)ZNetScene.instance == (Object)null)
			{
				return;
			}
			int num = 0;
			foreach (ZNetView item in ((Component)val).GetComponentsInChildren<ZNetView>(true).ToList())
			{
				ZDO zDO = item.GetZDO();
				if (zDO != null && zDO.GetInt("lyng_scenery_obj", 0) == 1)
				{
					if (!item.IsOwner())
					{
						item.ClaimOwnership();
					}
					ZNetScene.instance.Destroy(((Component)item).gameObject);
					num++;
				}
			}
			LyngbakrPlugin.Log.LogInfo((object)$"Cleared {num} scenery objects");
		}

		private static bool Peaceful(GameObject prefab)
		{
			if ((Object)(object)prefab.GetComponentInChildren<SpawnArea>(true) == (Object)null && (Object)(object)prefab.GetComponentInChildren<CreatureSpawner>(true) == (Object)null && (Object)(object)prefab.GetComponentInChildren<Character>(true) == (Object)null)
			{
				return (Object)(object)prefab.GetComponentInChildren<BaseAI>(true) == (Object)null;
			}
			return false;
		}

		internal IEnumerator Populate(int sig, Dictionary<string, int> dryRun = null)
		{
			LyngbakrBody lyng = LyngbakrBody.Instance;
			ZoneSystem instance = ZoneSystem.instance;
			WorldGenerator wg = WorldGenerator.instance;
			if ((Object)(object)lyng == (Object)null || (Object)(object)ZNetScene.instance == (Object)null || (Object)(object)Hmap == (Object)null || (Object)(object)instance == (Object)null)
			{
				yield break;
			}
			if (dryRun == null)
			{
				ClearScenery();
			}
			Random rng = new Random(sig);
			int half = W / 2;
			HHeights.Invoke(Hmap);
			List<int> insideIdx = (from i in Enumerable.Range(0, N * N)
				where _inside[i] && _edgeDist[i] > 3f
				select i).ToList();
			if (insideIdx.Count == 0)
			{
				if (dryRun == null)
				{
					ZDO zDO = _nview.GetZDO();
					if (zDO != null)
					{
						zDO.Set("lyng_scenery", sig);
					}
				}
				yield break;
			}
			float zones = (float)insideIdx.Count * 1f * 1f / (instance.m_zoneSize * instance.m_zoneSize);
			List<Vector2> blocked = new List<Vector2>();
			ZNetView[] componentsInChildren = ((Component)lyng.PieceRoot).GetComponentsInChildren<ZNetView>(true);
			int nearest;
			for (nearest = 0; nearest < componentsInChildren.Length; nearest++)
			{
				ZNetView val = componentsInChildren[nearest];
				if (dryRun == null || val.GetZDO() == null || val.GetZDO().GetInt("lyng_scenery_obj", 0) != 1)
				{
					Vector3 val2 = T.InverseTransformPoint(((Component)val).transform.position);
					blocked.Add(new Vector2(val2.x, val2.z));
				}
			}
			Biome present = (Biome)0;
			foreach (Biome region in _regions)
			{
				present |= region;
			}
			int placed = 0;
			int perFrame = 0;
			Vector3 val6 = default(Vector3);
			foreach (ZoneVegetation veg in instance.m_vegetation)
			{
				if (!veg.m_enable || (Object)(object)veg.m_prefab == (Object)null || (veg.m_biome & present) == 0 || !Peaceful(veg.m_prefab) || HasOwnPhysicsPrefab(veg.m_prefab))
				{
					continue;
				}
				float cosMax = Mathf.Cos(veg.m_maxTilt * ((float)Math.PI / 180f));
				float cosMin = Mathf.Cos(veg.m_minTilt * ((float)Math.PI / 180f));
				float num = ((veg.m_max < 1f) ? veg.m_max : ((veg.m_min + veg.m_max) * 0.5f)) * zones;
				int groups = Mathf.FloorToInt(num) + ((R() < num - Mathf.Floor(num)) ? 1 : 0);
				int g = 0;
				while (g < groups)
				{
					int num2 = insideIdx[rng.Next(insideIdx.Count)];
					Vector2 centre = new Vector2((float)(num2 % N - half) * 1f, (float)(num2 / N - half) * 1f);
					int size = rng.Next(Mathf.Max(1, veg.m_groupSizeMin), Mathf.Max(veg.m_groupSizeMin, veg.m_groupSizeMax) + 1);
					for (int m = 0; m < size; nearest = m++)
					{
						Vector2 val3 = centre;
						if (m > 0 || size > 1)
						{
							float num3 = R() * (float)Math.PI * 2f;
							float num4 = Mathf.Sqrt(R()) * veg.m_groupRadius;
							val3 += new Vector2(Mathf.Cos(num3), Mathf.Sin(num3)) * num4;
						}
						float num5 = R() * 360f;
						float num6 = (R() * 2f - 1f) * veg.m_randTilt;
						float num7 = (R() * 2f - 1f) * veg.m_randTilt;
						float num8 = Mathf.Lerp(veg.m_scaleMin, veg.m_scaleMax, R());
						bool flag = veg.m_chanceToUseGroundTilt > 0f && R() <= veg.m_chanceToUseGroundTilt;
						if (!SampleDeck(val3.x, val3.y, out var h, out var normal, out var nearest2) || !_inside[nearest2] || _edgeDist[nearest2] < 3f || _modH[nearest2] || NearBuild(val3))
						{
							continue;
						}
						Biome val4 = DeckLook.Biomes[_biomeAt[nearest2]];
						if ((veg.m_biome & val4) == 0)
						{
							continue;
						}
						Vector2 val5 = SourceOf(val3.x, val3.y);
						((Vector3)(ref val6))..ctor(val5.x, 0f, val5.y);
						if (wg != null && _patchwork && (veg.m_biomeArea & wg.GetBiomeArea(val6)) == 0)
						{
							continue;
						}
						float num9 = _srcAltitude[nearest2];
						if (num9 < veg.m_minAltitude || num9 > veg.m_maxAltitude)
						{
							continue;
						}
						if (veg.m_minVegetation != veg.m_maxVegetation)
						{
							float a = _mask[nearest2].a;
							if (a > veg.m_maxVegetation || a < veg.m_minVegetation)
							{
								continue;
							}
						}
						if (veg.m_minOceanDepth != veg.m_maxOceanDepth)
						{
							float num10 = Mathf.Max(0f, 0f - num9);
							if (num10 < veg.m_minOceanDepth || num10 > veg.m_maxOceanDepth)
							{
								continue;
							}
						}
						if (normal.y < cosMax || normal.y > cosMin)
						{
							continue;
						}
						if (veg.m_terrainDeltaRadius > 0f)
						{
							float num11 = float.MaxValue;
							float num12 = float.MinValue;
							for (int num13 = 0; num13 < 10; num13++)
							{
								float num14 = (float)num13 * (float)Math.PI * 2f / 10f;
								if (SampleDeck(val3.x + Mathf.Cos(num14) * veg.m_terrainDeltaRadius, val3.y + Mathf.Sin(num14) * veg.m_terrainDeltaRadius, out var h2, out var _, out nearest))
								{
									num11 = Mathf.Min(num11, h2);
									num12 = Mathf.Max(num12, h2);
								}
							}
							float num15 = ((num12 > num11) ? (num12 - num11) : 0f);
							if (num15 > veg.m_maxTerrainDelta || num15 < veg.m_minTerrainDelta)
							{
								continue;
							}
						}
						if (veg.m_inForest && _patchwork)
						{
							float forestFactor = WorldGenerator.GetForestFactor(val6);
							if (forestFactor < veg.m_forestTresholdMin || forestFactor > veg.m_forestTresholdMax)
							{
								continue;
							}
						}
						Quaternion val7 = (flag ? Quaternion.LookRotation(Vector3.Cross(normal, Quaternion.Euler(0f, num5, 0f) * Vector3.forward), normal) : Quaternion.Euler(num6, num5, num7));
						if (dryRun != null)
						{
							dryRun.TryGetValue(((Object)veg.m_prefab).name, out var value);
							dryRun[((Object)veg.m_prefab).name] = value + 1;
							continue;
						}
						Vector3 val8 = T.TransformPoint(new Vector3(val3.x, h + veg.m_groundOffset, val3.y));
						GameObject val9;
						try
						{
							val9 = Object.Instantiate<GameObject>(veg.m_prefab, val8, T.rotation * val7);
						}
						catch (Exception ex)
						{
							LyngbakrPlugin.Log.LogWarning((object)("Scenery " + ((Object)veg.m_prefab).name + " failed: " + ex.Message));
							break;
						}
						ZNetView component = val9.GetComponent<ZNetView>();
						if ((Object)(object)component != (Object)null && component.GetZDO() != null)
						{
							component.GetZDO().Set("lyng_scenery_obj", 1);
							if (!Mathf.Approximately(num8, val9.transform.localScale.x))
							{
								component.SetLocalScale(Vector3.one * num8);
							}
						}
						lyng.AttachObject(val9, log: false);
						placed++;
						nearest = perFrame + 1;
						perFrame = nearest;
						if (nearest >= 25)
						{
							perFrame = 0;
							yield return null;
						}
					}
					nearest = g++;
				}
			}
			if (dryRun == null)
			{
				ZDO zDO2 = _nview.GetZDO();
				if (zDO2 != null)
				{
					zDO2.Set("lyng_scenery", sig);
				}
				LyngbakrPlugin.Log.LogInfo((object)string.Format("Scenery: {0} objects placed by the game's vegetation rules for {1}", placed, string.Join(", ", _regions)));
			}
			bool NearBuild(Vector2 p)
			{
				//IL_0010: Unknown result type (might be due to invalid IL or missing references)
				//IL_0015: Unknown result type (might be due to invalid IL or missing references)
				//IL_0016: Unknown result type (might be due to invalid IL or missing references)
				//IL_001b: Unknown result type (might be due to invalid IL or missing references)
				foreach (Vector2 item in blocked)
				{
					Vector2 val10 = item - p;
					if (((Vector2)(ref val10)).sqrMagnitude < 9f)
					{
						return true;
					}
				}
				return false;
			}
			float R()
			{
				return (float)rng.NextDouble();
			}
		}

		private static bool HasOwnPhysicsPrefab(GameObject prefab)
		{
			return prefab.GetComponentsInChildren<Rigidbody>(true).Any((Rigidbody rb) => !rb.isKinematic);
		}

		internal void Build(Transform pieceRoot, ZNetView nview, List<Vector2> outline, float cutHeight)
		{
			//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_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_004b: 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_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: 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_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_00e8: Expected O, but got Unknown
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: 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_01a0: 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)
			_nview = nview;
			Instance = this;
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor(float.MaxValue, float.MaxValue);
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(float.MinValue, float.MinValue);
			foreach (Vector2 item in outline)
			{
				val = Vector2.Min(val, item);
				val2 = Vector2.Max(val2, item);
			}
			Vector2 val3 = (val + val2) * 0.5f;
			W = Mathf.Min(254, (Mathf.CeilToInt(Mathf.Max(val2.x - val.x, val2.y - val.y) / 1f) + 8) / 2 * 2);
			N = W + 1;
			GameObject val4 = new GameObject("LyngbakrDeck")
			{
				layer = LayerMask.NameToLayer("terrain")
			};
			val4.SetActive(false);
			T = val4.transform;
			T.SetParent(pieceRoot, false);
			T.localPosition = new Vector3(val3.x, cutHeight, val3.y);
			T.localRotation = Quaternion.identity;
			ZDO zDO = nview.GetZDO();
			BuiltKey = WorldSettings.DeckKey(zDO);
			_seed = WorldSettings.DeckSeed(zDO);
			_biomeChoices = WorldSettings.DeckBiomes(zDO);
			string text = WorldSettings.DeckBiome(zDO).Trim();
			_patchwork = text.Equals("Patchwork", StringComparison.OrdinalIgnoreCase);
			_biome = (Biome)(_patchwork ? 1 : ((int)ParseBiome(text)));
			_sector = MakeSector(_biome);
			_shell = (Collider)(object)(((Object)(object)pieceRoot.parent != (Object)null) ? ((IEnumerable<MeshCollider>)((Component)pieceRoot.parent).GetComponentsInChildren<MeshCollider>(true)).FirstOrDefault((Func<MeshCollider, bool>)((MeshCollider c) => ((Object)c).name == "mainbody")) : null);
			BuildBase(outline, val3, cutHeight);
			_level = new float[N * N];
			_smooth = new float[N * N];
			_modH = new bool[N * N];
			_paint = (Color[])(object)new Color[N * N];
			_modP = new bool[N * N];
			_mask = (Color[])_baseMask.Clone();
			if (!Scouting)
			{
				Load();
			}
			val4.AddComponent<MeshCollider>();
			val4.SetActive(true);
			Hmap = val4.AddComponent<Heightmap>();
			AllHeightmaps.Invoke().Remove(Hmap);
			Heightmap val5 = (((Object)(object)ZoneSystem.instance != (Object)null && (Object)(object)ZoneSystem.instance.m_zonePrefab != (Object)null) ? ZoneSystem.instance.m_zonePrefab.GetComponentInChildren<Heightmap>(true) : null);
			if ((Object)(object)val5 == (Object)null)
			{
				LyngbakrPlugin.Log.LogError((object)"Deck: no vanilla terrain to copy the material from");
				return;
			}
			Hmap.m_width = W;
			Hmap.m_scale = 1f;
			Hmap.m_material = DeckLook.MakeMaterial(val5.m_material);
			Hmap.m_terrainCompilerPrefab = val5.m_terrainCompilerPrefab;
			((Behaviour)Hmap).enabled = true;
			val4.AddComponent<NoHover>();
			_nview.Unregister("LyngbakrDeckOp");
			_nview.Register<ZPackage>("LyngbakrDeckOp", (Action<long, ZPackage>)RPC_Op);
			if (_nview.IsOwner())
			{
				((MonoBehaviour)this).StartCoroutine(EnsureScenery());
			}
			if (!Scouting)
			{
				LyngbakrPlugin.Log.LogInfo((object)string.Format("Deck terrain: {0} x {1} m, {2}, {3} m² of ground, {4} edited heights, {5} painted", W, W, _patchwork ? ("patchwork of " + string.Join(", ", _regions)) : ((object)Unsafe.As<Biome, Biome>(ref _biome)/*cast due to .constrained prefix*/).ToString(), Count(_inside), Count(_modH), Count(_modP)));
			}
		}

		private void OnDestroy()
		{
			if ((Object)(object)Instance == (Object)(object)this)
			{
				Instance = null;
			}
			if ((Object)(object)Hmap != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)Hmap).gameObject);
			}
		}

		private static int Count(bool[] a)
		{
			int num = 0;
			for (int i = 0; i < a.Length; i++)
			{
				if (a[i])
				{
					num++;
				}
			}
			return num;
		}

		private static Biome ParseBiome(string s)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				return (Biome)Enum.Parse(typeof(Biome), s.Trim(), ignoreCase: true);
			}
			catch
			{
				return (Biome)1;
			}
		}

		private static BiomeSector MakeSector(Biome biome)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Invalid comparison between Unknown and I4
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected O, but got Unknown
			if ((int)biome == 1 && BiomeSector.EmptyMeadows != null)
			{
				return BiomeSector.EmptyMeadows;
			}
			if ((int)biome == 8 && BiomeSector.EmptyBlackForest != null)
			{
				return BiomeSector.EmptyBlackForest;
			}
			BiomeSector val = (BiomeSector)AccessTools.CreateInstance(typeof(BiomeSector));
			val.Biome = biome;
			return val;
		}

		private static float Fbm(Vector2 p, float freq, int octaves, float seed)
		{
			//IL_0017: 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)
			float num = 1f;
			float num2 = 0f;
			float num3 = 0f;
			for (int i = 0; i < octaves; i++)
			{
				num2 += (Mathf.PerlinNoise(p.x * freq + seed * 13.1f + (float)i * 31.7f, p.y * freq + seed * 7.3f + (float)i * 17.9f) - 0.5f) * 2f * num;
				num3 += num;
				num *= 0.5f;
				freq *= 2.03f;
			}
			return num2 / num3;
		}

		internal Vector2 SourceOf(float lx, float lz)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			return _src + new Vector2(lx, lz);
		}

		private unsafe List<Biome> AllowedBiomes()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			List<Biome> list = new List<Biome>();
			string[] array = _biomeChoices.Split(new char[3] { ',', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string text in array)
			{
				Biome[] biomes = DeckLook.Biomes;
				for (int j = 0; j < biomes.Length; j++)
				{
					Biome item = biomes[j];
					if (((object)(*(Biome*)(&item))/*cast due to .constrained prefix*/).ToString().Equals(text.Trim(), StringComparison.OrdinalIgnoreCase) && !list.Contains(item))
					{
						list.Add(item);
					}
				}
			}
			if (list.Count == 0)
			{
				Biome[] array2 = new Biome[5];
				RuntimeHelpers.InitializeArray(array2, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
				list.AddRange((IEnumerable<Biome>)(object)array2);
			}
			return list;
		}

		private Vector2 FindSource(float size)
		{
			//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_024d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: 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_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Invalid comparison between Unknown and I4
			//IL_0139: 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_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0235: Unknown result type (might be due to invalid IL or missing references)
			WorldGenerator instance = WorldGenerator.instance;
			List<Biome> list = AllowedBiomes();
			Random random = new Random(_seed * 48271 + ((instance != null) ? instance.GetSeed() : 0));
			Vector2 result = Vector2.zero;
			float num = float.MinValue;
			for (int i = 0; i < 3000; i++)
			{
				if (instance == null)
				{
					break;
				}
				float num2 = (float)random.NextDouble() * (float)Math.PI * 2f;
				float num3 = 400f + (float)random.NextDouble() * 7600f;
				Vector2 val = new Vector2(Mathf.Cos(num2), Mathf.Sin(num2)) * num3;
				int num4 = 0;
				int num5 = 0;
				float num6 = float.MaxValue;
				float num7 = float.MinValue;
				HashSet<Biome> hashSet = new HashSet<Biome>();
				for (int j = 0; j < 7; j++)
				{
					for (int k = 0; k < 7; k++)
					{
						Vector2 val2 = val + new Vector2(((float)k / 6f - 0.5f) * size * 0.8f, ((float)j / 6f - 0.5f) * size * 0.8f);
						float height = instance.GetHeight(val2.x, val2.y);
						Biome biome = instance.GetBiome(val2.x, val2.y, 0.02f, false);
						if (height > 31f)
						{
							num4++;
						}
						if ((int)biome != 256)
						{
							if (!list.Contains(biome))
							{
								num5++;
							}
							else
							{
								hashSet.Add(biome);
							}
							num6 = Mathf.Min(num6, height);
							num7 = Mathf.Max(num7, height);
						}
					}
				}
				float num8 = (float)num4 / 49f;
				float num9 = ((num7 > num6) ? (num7 - num6) : 0f);
				float num10 = num8 * 4f - (float)num5 * 2f + ((hashSet.Count >= 2 && hashSet.Count <= 3) ? 3f : ((hashSet.Count == 1) ? 0.5f : 0f)) + Mathf.Clamp(num9, 0f, 60f) / 30f;
				if (num10 > num)
				{
					num = num10;
					result = val;
				}
				if (num8 > 0.85f && num5 == 0 && hashSet.Count >= 2 && hashSet.Count <= 3 && num9 > 12f)
				{
					return val;
				}
			}
			return result;
		}

		private static float Compress(float altitude)
		{
			if (altitude < 0.3f)
			{
				return 0.3f;
			}
			if (altitude <= 8f)
			{
				return altitude;
			}
			return 8f + Mathf.Min(24f, (altitude - 8f) * 0.35f);
		}

		private void BuildBase(List<Vector2> outline, Vector2 centre, float cutHeight)
		{
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: 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_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: 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_01b3: 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_0320: Unknown result type (might be due to invalid IL or missing references)
			//IL_0325: Unknown result type (might be due to invalid IL or missing references)
			//IL_0328: Unknown result type (might be due to invalid IL or missing references)
			//IL_032f: Unknown result type (might be due to invalid IL or missing references)
			//IL_033c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0341: Unknown result type (might be due to invalid IL or missing references)
			//IL_0344: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Unknown result type (might be due to invalid IL or missing references)
			//IL_034d: Unknown result type (might be due to invalid IL or missing references)
			//IL_036a: Unknown result type (might be due to invalid IL or missing references)
			//IL_020d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: Unknown result type (might be due to invalid IL or missing references)
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_05ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_05bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_0401: Unknown result type (might be due to invalid IL or missing references)
			//IL_053f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0544: Unknown result type (might be due to invalid IL or missing references)
			//IL_054f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0554: Unknown result type (might be due to invalid IL or missing references)
			//IL_0564: Unknown result type (might be due to invalid IL or missing references)
			//IL_0498: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_057c: 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)
			_base = new float[N * N];
			_inside = new bool[N * N];
			_baseMask = (Color[])(object)new Color[N * N];
			_weights = new float[N * N * 6];
			_biomeAt = new byte[N * N];
			_maxDig = new float[N * N];
			_maxRaise = new float[N * N];
			_digFloorPlus8 = new float[N * N];
			_edgeDist = new float[N * N];
			_srcAltitude = new float[N * N];
			int num = W / 2;
			WorldGenerator instance = WorldGenerator.instance;
			for (int i = 0; i < N * N; i++)
			{
				Vector2 p = centre + new Vector2((float)(i % N - num) * 1f, (float)(i / N - num) * 1f);
				_inside[i] = Inside(outline, p);
				_edgeDist[i] = EdgeDistance(outline, p);
			}
			int num2 = Mathf.Max(0, Array.IndexOf(DeckLook.Biomes, _biome));
			_src = ((_patchwork && instance != null) ? FindSource((float)W * 1f) : Vector2.zero);
			float num3 = 0f;
			if (_patchwork && instance != null)
			{
				List<float> list = new List<float>();
				for (int j = 0; j < N; j += 3)
				{
					for (int k = 0; k < N; k += 3)
					{
						if (_inside[j * N + k])
						{
							Vector2 val = SourceOf((float)(k - num) * 1f, (float)(j - num) * 1f);
							float num4 = instance.GetHeight(val.x, val.y) - 30f;
							if (num4 > 0.5f)
							{
								list.Add(num4);
							}
						}
					}
				}
				if (list.Count > 0)
				{
					list.Sort();
					num3 = Mathf.Max(0f, list[list.Count / 10] - 0.5f);
				}
			}
			float[] array = new float[N * N * 6];
			int[] count = new int[6];
			Color val2 = default(Color);
			Ray val4 = default(Ray);
			RaycastHit val5 = default(RaycastHit);
			for (int l = 0; l < N; l++)
			{
				for (int m = 0; m < N; m++)
				{
					int num5 = l * N + m;
					int num6 = num2;
					float num7 = 2f;
					((Color)(ref val2))..ctor(0f, 0f, 0f, 1f);
					if (_patchwork && instance != null)
					{
						Vector2 val3 = SourceOf((float)(m - num) * 1f, (float)(l - num) * 1f);
						Biome biome = instance.GetBiome(val3.x, val3.y, 0.02f, false);
						num7 = instance.GetBiomeHeight(biome, val3.x, val3.y, ref val2, false, true) - 30f;
						int num8 = Array.IndexOf(DeckLook.Biomes, biome);
						num6 = ((num8 >= 0) ? num8 : 0);
					}
					_srcAltitude[num5] = num7;
					array[num5 * 6 + num6] = 1f;
					if (_inside[num5])
					{
						count[num6]++;
					}
					float num9 = _edgeDist[num5];
					if (_inside[num5])
					{
						float num10 = 0.02f + 2.5f * (1f - Mathf.Exp((0f - num9) / 12f));
						float num11 = ((!_patchwork) ? (num10 + Fbm(new Vector2((float)m, (float)l), 0.022f, 3, (float)_seed * 0.137f) * 2f) : ((num7 < 0.3f) ? 0.3f : Compress(num7 - num3)));
						_base[num5] = Mathf.Lerp(num10, Mathf.Max(num11, 0.3f), Mathf.SmoothStep(0f, 1f, Mathf.Clamp01((num9 - 2f) / 16f)));
						_baseMask[num5] = new Color(0f, 0f, 0f, Mathf.Clamp01(val2.a));
						_maxDig[num5] = 8f * Mathf.Clamp01(num9 / 12f);
						_maxRaise[num5] = 8f;
						continue;
					}
					float num12 = 0f - Mathf.Min(cutHeight + 30f, 0.15f + num9 * 6f);
					if (num9 < 4f && (Object)(object)_shell != (Object)null)
					{
						float num13 = (float)(m - num) * 1f;
						float num14 = (float)(l - num) * 1f;
						((Ray)(ref val4))..ctor(T.TransformPoint(new Vector3(num13, 30f, num14)), -T.up);
						if (_shell.Raycast(val4, ref val5, 60f))
						{
							num12 = T.InverseTransformPoint(((RaycastHit)(ref val5)).point).y - 0.06f;
						}
					}
					_base[num5] = num12;
					_baseMask[num5] = new Color(0f, 0f, 0f, 0f);
				}
			}
			float[] array2 = new float[array.Length];
			for (int n = 0; n < 3; n++)
			{
				Array.Copy(array, array2, array.Length);
				for (int num15 = 1; num15 < N - 1; num15++)
				{
					for (int num16 = 1; num16 < N - 1; num16++)
					{
						for (int num17 = 0; num17 < 6; num17++)
						{
							float num18 = 0f;
							for (int num19 = -1; num19 <= 1; num19++)
							{
								for (int num20 = -1; num20 <= 1; num20++)
								{
									num18 += array2[((num15 + num19) * N + num16 + num20) * 6 + num17];
								}
							}
							array[(num15 * N + num16) * 6 + num17] = num18 / 9f;
						}
					}
				}
			}
			for (int num21 = 0; num21 < N * N; num21++)
			{
				float num22 = 0f;
				int num23 = 0;
				for (int num24 = 0; num24 < 6; num24++)
				{
					num22 += array[num21 * 6 + num24];
					if (array[num21 * 6 + num24] > array[num21 * 6 + num23])
					{
						num23 = num24;
					}
				}
				for (int num25 = 0; num25 < 6; num25++)
				{
					_weights[num21 * 6 + num25] = ((num22 > 0f) ? (array[num21 * 6 + num25] / num22) : ((num25 == 0) ? 1f : 0f));
				}
				_biomeAt[num21] = (byte)num23;
			}
			_regions.Clear();
			for (int num26 = 0; num26 < 6; num26++)
			{
				if (count[num26] > 0)
				{
					_regions.Add(DeckLook.Biomes[num26]);
				}
			}
			BiomeShare.Clear();
			for (int num27 = 0; num27 < 6; num27++)
			{
				if (count[num27] > 0)
				{
					BiomeShare[DeckLook.Biomes[num27]] = (float)count[num27] * 100f / (float)Math.Max(1, count.Sum());
				}
			}
			float[] array3 = (float[])_base.Clone();
			for (int num28 = 1; num28 < N - 1; num28++)
			{
				for (int num29 = 1; num29 < N - 1; num29++)
				{
					int num30 = num28 * N + num29;
					if (!_inside[num30] || _edgeDist[num30] < 3f)
					{
						continue;
					}
					float num31 = 0f;
					int num32 = 0;
					for (int num33 = -1; num33 <= 1; num33++)
					{
						for (int num34 = -1; num34 <= 1; num34++)
						{
							int num35 = num30 + num33 * N + num34;
							if (_inside[num35])
							{
								num31 += array3[num35];
								num32++;
							}
						}
					}
					_base[num30] = num31 / (float)num32;
				}
			}
			for (int num36 = 0; num36 < N * N; num36++)
			{
				_digFloorPlus8[num36] = _base[num36] - _maxDig[num36] + 8f;
			}
			if (!_patchwork || Scouting)
			{
				return;
			}
			float num37 = 0f;
			int num38 = 0;
			for (int num39 = 0; num39 < N * N; num39++)
			{
				if (_inside[num39])
				{
					num37 += _baseMask[num39].a;
					num38++;
				}
			}
			LyngbakrPlugin.Log.LogInfo((object)(string.Format("Deck copies the world around ({0:F0}, {1:F0}): {2}, ", _src.x, _src.y, string.Join(", ", _regions.Select((Biome r) => $"{r} {count[Array.IndexOf(DeckLook.Biomes, r)] * 100 / Math.Max(1, count.Sum())}%"))) + $"land lowered {num3:F1} m, vegetation mask {num37 / (float)Math.Max(1, num38):F2}"));
		}

		private static bool Inside(List<Vector2> seg, Vector2 p)
		{
			//IL_0008: 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_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: 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_0055: 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_0063: Unknown result type (might be due to invalid IL or missing references)
			bool flag = false;
			for (int i = 0; i + 1 < seg.Count; i += 2)
			{
				Vector2 val = seg[i];
				Vector2 val2 = seg[i + 1];
				if (val.y > p.y != val2.y > p.y && p.x < val.x + (p.y - val.y) / (val2.y - val.y) * (val2.x - val.x))
				{
					flag = !flag;
				}
			}
			return flag;
		}

		private static float EdgeDistance(List<Vector2> seg, Vector2 p)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_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)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			float num = float.MaxValue;
			for (int i = 0; i + 1 < seg.Count; i += 2)
			{
				Vector2 val = seg[i];
				Vector2 val2 = seg[i + 1] - val;
				float num2 = ((((Vector2)(ref val2)).sqrMagnitude > 1E-06f) ? Mathf.Clamp01(Vector2.Dot(p - val, val2) / ((Vector2)(ref val2)).sqrMagnitude) : 0f);
				float num3 = num;
				Vector2 val3 = p - (val + val2 * num2);
				num = Mathf.Min(num3, ((Vector2)(ref val3)).sqrMagnitude);
			}
			return Mathf.Sqrt(num);
		}

		private void ToVertex(Vector3 local, out int x, out int y)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			x = Mathf.FloorToInt(local.x / 1f + 0.5f) + W / 2;
			y = Mathf.FloorToInt(local.z / 1f + 0.5f) + W / 2;
		}

		private void ToVertexMask(Vector3 local, out int x, out int y)
		{
			//IL_0001: 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)
			x = Mathf.FloorToInt(local.x / 1f + 0.5f + (float)((W + 1) / 2));
			y = Mathf.FloorToInt(local.z / 1f + 0.5f + (float)((W + 1) / 2));
		}

		private bool InGrid(int x, int y)
		{
			if (x >= 0 && y >= 0 && x < N)
			{
				return y < N;
			}
			return false;
		}

		internal bool Contains(Vector3 world, float r = 0f, float above = 40f)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: 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_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Hmap == (Object)null || !((Behaviour)Hmap).enabled)
			{
				return false;
			}
			Vector3 val = T.InverseTransformPoint(world);
			ToVertex(val, out var x, out var y);
			int num = Mathf.CeilToInt(r / 1f);
			x = Mathf.Clamp(x, -1, N);
			y = Mathf.Clamp(y, -1, N);
			bool flag = false;
			for (int i = -num; i <= num; i++)
			{
				if (flag)
				{
					break;
				}
				for (int j = -num; j <= num; j++)
				{
					if (flag)
					{
						break;
					}
					if (InGrid(x + j, y + i) && _inside[(y + i) * N + x + j])
					{
						flag = true;
					}
				}
			}
			if (!flag)
			{
				return false;
			}
			int num2 = Mathf.Clamp(x, 0, N - 1);
			int num3 = Mathf.Clamp(y, 0, N - 1);
			float num4 = HHeights.Invoke(Hmap)[num3 * N + num2];
			if (val.y > num4 - 6f - r)
			{
				return val.y < num4 + above;
			}
			return false;
		}

		private bool Generate()
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//IL_0159: 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_015e: Unknown result type (might be due to invalid IL or missing references)
			HInitialize(Hmap);
			List<float> list = HHeights.Invoke(Hmap);
			if (HBuild.Invoke(Hmap) == null)
			{
				HMBuildData val = new HMBuildData(T.position, W, 1f, false, WorldGenerator.instance);
				val.m_baseHeights = new List<float>(_base);
				val.m_baseMask = _baseMask;
				val.m_cornerBiomes = (BiomeSector[])(object)new BiomeSector[4] { _sector, _sector, _sector, _sector };
				HMBuildData val2 = val;
				HBuild.Invoke(Hmap) = val2;
				HCorners.Invoke(Hmap) = val2.m_cornerBiomes;
			}
			for (int i = 0; i < _base.Length; i++)
			{
				float num = _base[i];
				if (_modH[i])
				{
					num = Mathf.Clamp(num + _level[i] + _smooth[i], _base[i] - _maxDig[i], _base[i] + _maxRaise[i]);
				}
				list[i] = num;
				_mask[i] = (_modP[i] ? _paint[i] : _baseMask[i]);
			}
			Texture2D obj = HPaint.Invoke(Hmap);
			obj.SetPixels(_mask);
			obj.Apply();
			GrassGroundChanged();
			return true;
		}

		private void Update()
		{
			if ((Object)(object)_nview == (Object)null || !_nview.IsValid() || (Object)(object)Hmap == (Object)null)
			{
				return;
			}
			_pollTimer -= Time.deltaTime;
			if (!(_pollTimer > 0f))
			{
				_pollTimer = 0.25f;
				if (_nview.GetZDO().DataRevision != _loadedRevision && Load())
				{
					Hmap.Poke(1, false);
				}
			}
		}

		private bool Load()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_0155: 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)
			ZDO zDO = _nview.GetZDO();
			_loadedRevision = zDO.DataRevision;
			byte[] byteArray = zDO.GetByteArray("lyng_deck", (byte[])null);
			if (byteArray == null)
			{
				return false;
			}
			try
			{
				ZPackage val = new ZPackage(Decompress(byteArray));
				int num = val.ReadInt();
				int num2 = val.ReadInt();
				if (num != 1 || num2 != W)
				{
					LyngbakrPlugin.Log.LogWarning((object)$"Deck edits were saved for a {num2} m deck; this deck is {W} m, so they're ignored");
					return false;
				}
				Array.Clear(_modH, 0, _modH.Length);
				Array.Clear(_modP, 0, _modP.Length);
				Array.Clear(_level, 0, _level.Length);
				Array.Clear(_smooth, 0, _smooth.Length);
				int num3 = val.ReadInt();
				for (int i = 0; i < num3; i++)
				{
					int num4 = val.ReadInt();
					_modH[num4] = true;
					_level[num4] = val.ReadSingle();
					_smooth[num4] = val.ReadSingle();
				}
				int num5 = val.ReadInt();
				for (int j = 0; j < num5; j++)
				{
					int num6 = val.ReadInt();
					_modP[num6] = true;
					_paint[num6] = new Color(val.ReadSingle(), val.ReadSingle(), val.ReadSingle(), val.ReadSingle());
				}
				return true;
			}
			catch (Exception ex)
			{
				LyngbakrPlugin.Log.LogError((object)("Couldn't read the deck edits: " + ex.Message));
				return false;
			}
		}

		private void Save()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			ZPackage val = new ZPackage();
			val.Write(1);
			val.Write(W);
			val.Write(Count(_modH));
			for (int i = 0; i < _modH.Length; i++)
			{
				if (_modH[i])
				{
					val.Write(i);
					val.Write(_level[i]);
					val.Write(_smooth[i]);
				}
			}
			val.Write(Count(_modP));
			for (int j = 0; j < _modP.Length; j++)
			{
				if (_modP[j])
				{
					val.Write(j);
					Color val2 = _paint[j];
					val.Write(val2.r);
					val.Write(val2.g);
					val.Write(val2.b);
					val.Write(val2.a);
				}
			}
			ZDO zDO = _nview.GetZDO();
			zDO.Set("lyng_deck", Compress(val.GetArray()));
			_loadedRevision = zDO.DataRevision;
		}

		private static byte[] Compress(byte[] data)
		{
			using MemoryStream memoryStream = new MemoryStream();
			using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
			{
				gZipStream.Write(data, 0, data.Length);
			}
			return memoryStream.ToArray();
		}

		private static byte[] Decompress(byte[] data)
		{
			using GZipStream gZipStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress);
			using MemoryStream memoryStream = new MemoryStream();
			gZipStream.CopyTo(memoryStream);
			return memoryStream.ToArray();
		}

		internal void RequestOp(TerrainOp op)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//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_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			ZPackage val = new ZPackage();
			val.Write(T.InverseTransformPoint(((Component)op).transform.position));
			val.Write(op.m_settings.m_rotation);
			if (op.m_settings.m_rotation)
			{
				val.Write(T.InverseTransformDirection(((Component)op).transform.forward));
			}
			op.m_settings.Serialize(val, ((Component)op).gameObject);
			_nview.InvokeRPC("LyngbakrDeckOp", new object[1] { val });
		}

		private void RPC_Op(long sender, ZPackage pkg)
		{
			//IL_001d: 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_0033: 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_0038: 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_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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_00a4: 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_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_00d4: 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)
			if (!_nview.IsOwner() || (Object)(object)Hmap == (Object)null)
			{
				return;
			}
			Vector3 val = pkg.ReadVector3();
			Vector3 fwd = (pkg.ReadBool() ? pkg.ReadVector3() : Vector3.zero);
			Settings val2 = Settings.Deserialize(pkg);
			if (val2 != null)
			{
				if (val2.m_level)
				{
					Level(val + Vector3.up * val2.m_levelOffset, val2.m_levelRadius, val2.m_square);
				}
				if (val2.m_raise)
				{
					Raise(val, val2.m_raiseRadius, val2.m_raiseDelta, val2.m_square, val2.m_raisePower);
				}
				if (val2.m_smooth)
				{
					Smooth(val + Vector3.up * val2.m_levelOffset, val2.m_smoothRadius, val2.m_smoothPower);
				}
				if (val2.m_paintCleared)
				{
					Paint(val, fwd, val2);
				}
				Save();
				bool flag = val2.m_paintCleared && !val2.m_level && !val2.m_raise && !val2.m_smooth;
				Hmap.Poke(1, flag);
			}
		}

		private float H(int i)
		{
			return HHeights.Invoke(Hmap)[i];
		}

		private void Level(Vector3 l, float radius, bool square)
		{
			//IL_0001: 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_0044: 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)
			ToVertex(l, out var x, out var y);
			float num = radius / 1f;
			int num2 = Mathf.CeilToInt(num);
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor((float)x, (float)y);
			for (int i = y - num2; i <= y + num2; i++)
			{
				for (int j = x - num2; j <= x + num2; j++)
				{
					if ((square || !(Vector2.Distance(val, new Vector2((float)j, (float)i)) > num)) && InGrid(j, i))
					{
						int num3 = i * N + j;
						float num4 = l.y - H(num3) + _smooth[num3];
						_smooth[num3] = 0f;
						_level[num3] = Mathf.Clamp(_level[num3] + num4, -8f, 8f);
						_modH[num3] = true;
					}
				}
			}
		}

		private void Raise(Vector3 l, float radius, float delta, bool square, float power)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			ToVertex(l, out var x, out var y);
			float num = radius / 1f;
			int num2 = Mathf.CeilToInt(num);
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor((float)x, (float)y);
			for (int i = y - num2; i <= y + num2; i++)
			{
				for (int j = x - num2; j <= x + num2; j++)
				{
					if (!InGrid(j, i))
					{
						continue;
					}
					float num3 = 1f;
					if (!square)
					{
						float num4 = Vector2.Distance(val, new Vector2((float)j, (float)i));
						if (num4 > num)
						{
							continue;
						}
						if (power > 0f)
						{
							num3 = 1f - num4 / num;
							if (power != 1f)
							{
								num3 = Mathf.Pow(num3, power);
							}
						}
					}
					int num5 = i * N + j;
					float num6 = H(num5);
					float num7 = delta * num3;
					float num8 = l.y + num7;
					if (delta < 0f && num8 > num6)
					{
						continue;
					}
					if (delta > 0f)
					{
						if (num8 < num6)
						{
							continue;
						}
						if (num8 > num6 + num7)
						{
							num8 = num6 + num7;
						}
					}
					float num9 = num8 - num6 + _smooth[num5];
					_smooth[num5] = 0f;
					_level[num5] = Mathf.Clamp(_level[num5] + num9, -8f, 8f);
					_modH[num5] = true;
				}
			}
		}

		private void Smooth(Vector3 l, float radius, float power)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: 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)
			ToVertex(l, out var x, out var y);
			float num = radius / 1f;
			int num2 = Mathf.CeilToInt(num);
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor((float)x, (float)y);
			for (int i = y - num2; i <= y + num2; i++)
			{
				for (int j = x - num2; j <= x + num2; j++)
				{
					float num3 = Vector2.Distance(val, new Vector2((float)j, (float)i));
					if (!(num3 > num) && InGrid(j, i))
					{
						float num4 = num3 / num;
						num4 = ((power == 3f) ? (num4 * num4 * num4) : Mathf.Pow(num4, power));
						int num5 = i * N + j;
						float num6 = H(num5);
						_smooth[num5] = Mathf.Clamp(_smooth[num5] + Mathf.Lerp(num6, l.y, 1f - num4) - num6, -1f, 1f);
						_modH[num5] = true;
					}
				}
			}
		}

		private void Paint(Vector3 l, Vector3 fwd, Settings s)
		{
			//IL_0036: 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_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected I4, but got Unknown
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: 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_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_031b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0322: Unknown result type (might be due to invalid IL or missing references)
			//IL_032b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0342: Unknown result type (might be due to invalid IL or missing references)
			//IL_0349: Unknown result type (might be due to invalid IL or missing references)
			//IL_0352: Unknown result type (might be due to invalid IL or missing references)
			//IL_0369: Unknown result type (might be due to invalid IL or missing references)
			//IL_0370: Unknown result type (might be due to invalid IL or missing references)
			//IL_0379: Unknown result type (might be due to invalid IL or missing references)
			//IL_0396: Unknown result type (might be due to invalid IL or missing references)
			//IL_039c: Invalid comparison between Unknown and I4
			//IL_023c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: 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_01f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c9: 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_03d8: Unknown result type (might be due to invalid IL or missing references)
			if (s.m_halfOffset)
			{
				l.x -= 0.5f;
				l.z -= 0.5f;
			}
			ToVertexMask(l, out var x, out var y);
			float num = s.m_paintRadius / 1f;
			int num2 = Mathf.CeilToInt(num);
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor((float)x, (float)y);
			Color val2 = (InGrid(x, y) ? _mask[y * N + x] : Color.black);
			Color val3 = (Color)((int)s.m_paintType switch
			{
				0 => Heightmap.m_paintMaskDirt, 
				1 => Heightmap.m_paintMaskCultivated, 
				2 => Heightmap.m_paintMaskPaved, 
				3 => Heightmap.m_paintMaskNothing, 
				4 => Heightmap.m_paintMaskClearVegetation, 
				_ => Heightmap.m_paintMaskDeepSnow, 
			});
			for (int i = Mathf.Max(0, y - num2); i <= Mathf.Min(W, y + num2); i++)
			{
				for (int j = Mathf.Max(0, x - num2); j <= Mathf.Min(W, x + num2); j++)
				{
					int num3 = i * N + j;
					if (s.m_paintHeightCheck && H(num3) > l.y)
					{
						continue;
					}
					float num4 = Vector2.Distance(val, new Vector2((float)j, (float)i)) / num;
					float num5 = ((s.m_paintCurve != null && s.m_paintCurve.length != 0) ? s.m_paintCurve.Evaluate(num4) : Mathf.Pow(1f - Mathf.Clamp01(num4), s.m_paintExp));
					num5 *= s.m_paintStrength;
					if (s.m_sides && s.m_rotation)
					{
						float num6 = num5;
						Vector3 val4 = new Vector3((float)j - val.x, 0f, (float)i - val.y);
						num5 = num6 * (1f - Mathf.Abs(Vector3.Dot(fwd, ((Vector3)(ref val4)).normalized)));
					}
					Color val5 = _mask[num3];
					float a = val5.a;
					float clampMedian = 0f;
					if (num5 > 0f && val5.g > 0f && s.m_addMedianMax > 0f)
					{
						List<float> list = new List<float>(8);
						for (int k = -1; k <= 1; k++)
						{
							for (int m = -1; m <= 1; m++)
							{
								if ((m != 0 || k != 0) && j + m >= 0 && j + m <= W && i + k >= 0 && i + k <= W)
								{
									list.Add(_mask[(i + k) * N + j + m].g);
								}
							}
						}
						list.Sort();
						if (list.Count > 0)
						{
							clampMedian = list[list.Count / 2] * s.m_addMedianMax;
						}
					}
					val5.r = Mod(val5.r, val3.r, num5, val2.r);
					val5.g = Mod(val5.g, val3.g, num5, val2.g);
					val5.b = Mod(val5.b, val3.b, num5, val2.b);
					val5.a = (((int)s.m_paintType == 4) ? Mathf.Lerp(a, 0f, num5) : a);
					_modP[num3] = true;
					_paint[num3] = val5;
					_mask[num3] = val5;
					float Mod(float from, float to, float value, float centreFactor)
					{
						if (s.m_centerMultiplicationFactor > 0f)
						{
							value = value * (1f - s.m_centerMultiplicationFactor) + value * s.m_centerMultiplicationFactor * centreFactor;
						}
						if (s.m_pointMultiplicationFactor > 0f)
						{
							value = value * (1f - s.m_pointMultiplicationFactor) + value * s.m_pointMultiplicationFactor * from;
						}
						float num7 = Mathf.Lerp(from, to, value);
						if (clampMedian > 0f && num7 > from)
						{
							num7 = Mathf.Min(num7, clampMedian);
						}
						return num7;
					}
				}
			}
		}

		private static bool Is(Heightmap h)
		{
			if ((Object)(object)Instance != (Object)null)
			{
				return (Object)(object)Instance.Hmap == (Object)(object)h;
			}
			return false;
		}

		private static bool WorldHeight(float[] or, Vector3 worldPos, out float height)
		{
			//IL_000c: 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_0012: Unknown resu