Decompiled source of LudvigValheim v2.1.0

plugins/LudvigValheim/LudvigValheim.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[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 LudvigValheim
{
	internal static class BuildPieces
	{
		private static GameObject storage;

		private static GameObject car;

		private static GameObject hut;

		private static ZNetScene registered;

		internal static void Install(Harmony h)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			TempoInteraction.Install(h);
			HarmonyMethod val = new HarmonyMethod(typeof(BuildPieces), "TryRegister", (Type[])null);
			h.Patch((MethodBase)AccessTools.Method(typeof(ZNetScene), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			h.Patch((MethodBase)AccessTools.Method(typeof(ZNetScene), "Update", (Type[])null, (Type[])null), val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			h.Patch((MethodBase)AccessTools.Method(typeof(ObjectDB), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			h.Patch((MethodBase)AccessTools.Method(typeof(ObjectDB), "CopyOtherDB", (Type[])null, (Type[])null), (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		internal static void TryRegister()
		{
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Expected O, but got Unknown
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Unknown result type (might be due to invalid IL or missing references)
			//IL_0292: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Expected O, but got Unknown
			//IL_02cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0316: Unknown result type (might be due to invalid IL or missing references)
			ZNetScene instance = ZNetScene.instance;
			ObjectDB instance2 = ObjectDB.instance;
			if ((Object)(object)instance == (Object)null || (Object)(object)instance2 == (Object)null || (Object)(object)Plugin.Instance == (Object)null)
			{
				return;
			}
			GameObject itemPrefab = instance2.GetItemPrefab("Hammer");
			GameObject prefab = instance.GetPrefab("Cart");
			GameObject prefab2 = instance.GetPrefab("piece_chest_wood");
			if ((Object)(object)itemPrefab == (Object)null || (Object)(object)prefab == (Object)null || (Object)(object)prefab2 == (Object)null || (((Object)(object)car == (Object)null || (Object)(object)hut == (Object)null) && (Object)(object)Model.FindTemplate() == (Object)null))
			{
				return;
			}
			PieceTable buildPieces = itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared.m_buildPieces;
			if ((Object)(object)buildPieces == (Object)null)
			{
				return;
			}
			if ((Object)(object)storage == (Object)null)
			{
				storage = new GameObject("Ludvig_BuildTemplates");
				storage.SetActive(false);
				Object.DontDestroyOnLoad((Object)(object)storage);
			}
			if ((Object)(object)car == (Object)null)
			{
				car = Object.Instantiate<GameObject>(prefab2, storage.transform);
				((Object)car).name = "Ludvig_TempoGigante";
				car.layer = prefab.layer;
				Component[] components = car.GetComponents<Component>();
				foreach (Component val in components)
				{
					if (!(val is Transform) && !(val is ZNetView) && !(val is Piece) && !(val is Container) && !(val is WearNTear))
					{
						Object.DestroyImmediate((Object)(object)val);
					}
				}
				Vagon component = car.GetComponent<Vagon>();
				if ((Object)(object)component != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)component);
				}
				foreach (Transform item in Children(car.transform))
				{
					Object.DestroyImmediate((Object)(object)((Component)item).gameObject);
				}
				Collider[] components2 = car.GetComponents<Collider>();
				for (int i = 0; i < components2.Length; i++)
				{
					Object.DestroyImmediate((Object)(object)components2[i]);
				}
				DisableInPlacementGhost[] components3 = car.GetComponents<DisableInPlacementGhost>();
				for (int i = 0; i < components3.Length; i++)
				{
					Object.DestroyImmediate((Object)(object)components3[i]);
				}
				BoxCollider val2 = car.AddComponent<BoxCollider>();
				val2.center = new Vector3(0f, 1.1f, 0f);
				val2.size = new Vector3(1.55f, 0.65f, 3.25f);
				((Collider)val2).sharedMaterial = new PhysicsMaterial("Tempo tires")
				{
					staticFriction = 0.1f,
					dynamicFriction = 0.1f,
					bounciness = 0f,
					frictionCombine = (PhysicsMaterialCombine)2
				};
				for (int j = 0; j < 4; j++)
				{
					GameObject val3 = new GameObject("Tempo wheel collision " + j);
					val3.transform.SetParent(car.transform, false);
					val3.transform.localPosition = new Vector3((j % 2 == 0) ? (-0.94f) : 0.94f, 0.5f, (j < 2) ? 1.35f : (-1.35f));
					SphereCollider obj = val3.AddComponent<SphereCollider>();
					obj.radius = 0.45f;
					((Collider)obj).sharedMaterial = ((Collider)val2).sharedMaterial;
				}
				Rigidbody val4 = car.GetComponent<Rigidbody>();
				if ((Object)(object)val4 == (Object)null)
				{
					val4 = car.AddComponent<Rigidbody>();
				}
				val4.mass = 260f;
				val4.constraints = (RigidbodyConstraints)0;
				val4.useGravity = true;
				val4.isKinematic = false;
				val4.collisionDetectionMode = (CollisionDetectionMode)2;
				ZSyncTransform val5 = car.GetComponent<ZSyncTransform>();
				if ((Object)(object)val5 == (Object)null)
				{
					val5 = car.AddComponent<ZSyncTransform>();
				}
				val5.m_syncPosition = true;
				val5.m_syncRotation = true;
				val5.m_syncBodyVelocity = true;
				Container val6 = car.GetComponent<Container>();
				if ((Object)(object)val6 == (Object)null)
				{
					val6 = car.AddComponent<Container>();
				}
				val6.m_name = "IL Tempo Gigante — bagasjerom";
				val6.m_width = 6;
				val6.m_height = 4;
				val6.m_autoDestroyEmpty = false;
				val6.m_wagon = null;
				val6.m_rootObjectOverride = null;
				val6.m_open = null;
				val6.m_closed = null;
				car.AddComponent<TempoVehicle>().BuildVisual();
				car.SetActive(true);
				Configure(car, "IL Tempo Gigante", "Kjørbar bil med 24 plasser i bagasjerommet. Bruk førersetet for å kjøre.", instance2, new string[4] { "Copper", "Wood", "Stone", "BronzeNails" }, new int[4]
				{
					Plugin.Instance.CarCopper.Value,
					Plugin.Instance.CarWood.Value,
					Plugin.Instance.CarStone.Value,
					Plugin.Instance.CarNails.Value
				}, prefab.GetComponent<Piece>().m_icon);
			}
			if ((Object)(object)hut == (Object)null)
			{
				hut = Object.Instantiate<GameObject>(prefab2, storage.transform);
				((Object)hut).name = "Ludvig_Hut";
				Container component2 = hut.GetComponent<Container>();
				if ((Object)(object)component2 != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)component2);
				}
				foreach (Transform item2 in Children(hut.transform))
				{
					Object.DestroyImmediate((Object)(object)((Component)item2).gameObject);
				}
				Collider[] components2 = hut.GetComponents<Collider>();
				for (int i = 0; i < components2.Length; i++)
				{
					Object.DestroyImmediate((Object)(object)components2[i]);
				}
				hut.AddComponent<LudvigHome>().BuildVisual();
				Configure(hut, "Ludvigs hytte", "En liten hytte for Ludvig. Velg hjem ved å bruke hytta.", instance2, new string[2] { "Wood", "Stone" }, new int[2]
				{
					Plugin.Instance.HutWood.Value,
					Plugin.Instance.HutStone.Value
				}, prefab2.GetComponent<Piece>().m_icon);
			}
			if ((Object)(object)registered != (Object)(object)instance)
			{
				Register(instance, car);
				Register(instance, hut);
				registered = instance;
				Plugin.Instance.LogInfo("Ludvig build pieces registered: driveable Tempo Gigante (24 slots) and hut.");
			}
			RefreshCosts();
			if (!buildPieces.m_pieces.Contains(car))
			{
				buildPieces.m_pieces.Add(car);
			}
			if (!buildPieces.m_pieces.Contains(hut))
			{
				buildPieces.m_pieces.Add(hut);
			}
		}

		private static List<Transform> Children(Transform t)
		{
			List<Transform> list = new List<Transform>();
			for (int i = 0; i < t.childCount; i++)
			{
				list.Add(t.GetChild(i));
			}
			return list;
		}

		internal static void RefreshCosts()
		{
			Plugin instance = Plugin.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				SetCosts(car, new ConfigEntry<int>[4] { instance.CarCopper, instance.CarWood, instance.CarStone, instance.CarNails });
				SetCosts(hut, new ConfigEntry<int>[2] { instance.HutWood, instance.HutStone });
			}
		}

		private static void SetCosts(GameObject prefab, ConfigEntry<int>[] costs)
		{
			if ((Object)(object)prefab == (Object)null)
			{
				return;
			}
			Piece component = prefab.GetComponent<Piece>();
			if (!((Object)(object)component == (Object)null) && component.m_resources != null && component.m_resources.Length == costs.Length)
			{
				for (int i = 0; i < costs.Length; i++)
				{
					component.m_resources[i].m_amount = ((!Plugin.Instance.FreeTests) ? Plugin.Instance.MaterialCost(costs[i]) : 0);
				}
			}
		}

		private static void Configure(GameObject go, string name, string description, ObjectDB db, string[] items, int[] amounts, Sprite icon)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Expected O, but got Unknown
			ZNetView component = go.GetComponent<ZNetView>();
			component.m_persistent = true;
			component.m_type = (ObjectType)0;
			Piece val = go.GetComponent<Piece>();
			if ((Object)(object)val == (Object)null)
			{
				val = go.AddComponent<Piece>();
			}
			val.m_name = name;
			val.m_description = description;
			val.m_icon = icon;
			val.m_enabled = true;
			val.m_category = (PieceCategory)0;
			val.m_craftingStation = null;
			val.m_groundOnly = true;
			WearNTear component2 = go.GetComponent<WearNTear>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.m_new = null;
				component2.m_worn = null;
				component2.m_broken = null;
				component2.m_wet = null;
				component2.m_noSupportWear = false;
				component2.m_noRoofWear = false;
				component2.m_autoCreateFragments = false;
				if (((Object)go).name == "Ludvig_TempoGigante")
				{
					component2.m_staticPosition = false;
					component2.m_supports = false;
				}
			}
			val.m_resources = (Requirement[])(object)new Requirement[items.Length];
			for (int i = 0; i < items.Length; i++)
			{
				val.m_resources[i] = new Requirement
				{
					m_resItem = db.GetItemPrefab(items[i]).GetComponent<ItemDrop>(),
					m_amount = ((!Plugin.Instance.FreeTests) ? amounts[i] : 0),
					m_recover = true
				};
			}
		}

		private static void Register(ZNetScene scene, GameObject go)
		{
			((Dictionary<int, GameObject>)AccessTools.Field(typeof(ZNetScene), "m_namedPrefabs").GetValue(scene))[StringExtensionMethods.GetStableHashCode(((Object)go).name)] = go;
			if (!scene.m_prefabs.Contains(go))
			{
				scene.m_prefabs.Add(go);
			}
		}
	}
	internal sealed class LudvigHome : MonoBehaviour, Hoverable, Interactable
	{
		private static readonly List<LudvigHome> homes = new List<LudvigHome>();

		[SerializeField]
		private bool built;

		private readonly List<Mesh> meshes = new List<Mesh>();

		private readonly List<Material> materials = new List<Material>();

		internal Vector3 HomePosition => ((Component)this).transform.TransformPoint(new Vector3(0f, 0.15f, -0.4f));

		internal static LudvigHome Nearest(Vector3 position, float radius)
		{
			//IL_002d: 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)
			LudvigHome result = null;
			foreach (LudvigHome home in homes)
			{
				if (!((Object)(object)home == (Object)null) && ((Component)home).gameObject.activeInHierarchy)
				{
					float num = Vector3.Distance(position, ((Component)home).transform.position);
					if (num < radius)
					{
						radius = num;
						result = home;
					}
				}
			}
			return result;
		}

		private void OnEnable()
		{
			if (!homes.Contains(this))
			{
				homes.Add(this);
			}
		}

		private void OnDisable()
		{
			homes.Remove(this);
		}

		private void Update()
		{
			if (!built && !((Object)(object)Plugin.Instance == (Object)null) && !((Object)(object)Model.FindTemplate() == (Object)null))
			{
				BuildVisual();
			}
		}

		internal void BuildVisual()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_0229: Unknown result type (might be due to invalid IL or missing references)
			//IL_023d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0261: Unknown result type (might be due to invalid IL or missing references)
			//IL_0275: Unknown result type (might be due to invalid IL or missing references)
			//IL_0299: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
			built = true;
			Dictionary<string, Mesh> shapes = Geometry.Load(meshes, Plugin.Instance);
			Material mat = new Material(Model.FindTemplate());
			materials.Add(mat);
			((Object)mat).name = "Ludvig_Hut_Wood";
			if (mat.HasProperty("_Color"))
			{
				mat.SetColor("_Color", new Color(0.28f, 0.15f, 0.065f));
			}
			if (mat.HasProperty("_MainTex"))
			{
				mat.SetTexture("_MainTex", (Texture)(object)Texture2D.whiteTexture);
			}
			Action<string, Vector3, Vector3, float> obj = delegate(string n, Vector3 pos, Vector3 size, float angle)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0023: Unknown result type (might be due to invalid IL or missing references)
				//IL_0029: Unknown result type (might be due to invalid IL or missing references)
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0035: Unknown result type (might be due to invalid IL or missing references)
				//IL_0047: Unknown result type (might be due to invalid IL or missing references)
				//IL_0051: Unknown result type (might be due to invalid IL or missing references)
				//IL_006c: Unknown result type (might be due to invalid IL or missing references)
				GameObject val = new GameObject(n);
				val.transform.SetParent(((Component)this).transform, false);
				val.transform.localPosition = pos;
				val.transform.localScale = size;
				val.transform.localRotation = Quaternion.Euler(0f, 0f, angle);
				val.AddComponent<MeshFilter>().sharedMesh = shapes["Cube"];
				((Renderer)val.AddComponent<MeshRenderer>()).sharedMaterial = mat;
				val.AddComponent<BoxCollider>();
			};
			obj("floor", new Vector3(0f, 0f, 0f), new Vector3(3f, 0.18f, 3f), 0f);
			obj("back", new Vector3(0f, 1.2f, 1.45f), new Vector3(3f, 2.4f, 0.15f), 0f);
			obj("left", new Vector3(-1.45f, 1.2f, 0f), new Vector3(0.15f, 2.4f, 3f), 0f);
			obj("right", new Vector3(1.45f, 1.2f, 0f), new Vector3(0.15f, 2.4f, 3f), 0f);
			obj("door_left", new Vector3(-1.15f, 1.2f, -1.45f), new Vector3(0.7f, 2.4f, 0.15f), 0f);
			obj("door_right", new Vector3(1.15f, 1.2f, -1.45f), new Vector3(0.7f, 2.4f, 0.15f), 0f);
			obj("lintel", new Vector3(0f, 2.35f, -1.45f), new Vector3(1.6f, 0.3f, 0.15f), 0f);
			obj("roof_left", new Vector3(-0.82f, 2.8f, 0f), new Vector3(2f, 0.18f, 3.5f), 30f);
			obj("roof_right", new Vector3(0.82f, 2.8f, 0f), new Vector3(2f, 0.18f, 3.5f), -30f);
			obj("bed", new Vector3(0.65f, 0.24f, 0.65f), new Vector3(1.2f, 0.35f, 1.3f), 0f);
		}

		public string GetHoverName()
		{
			return "Ludvigs hytte";
		}

		public string GetHoverText()
		{
			return "[E] Velg denne hytta som Ludvigs hjem";
		}

		public float GetHoverOffset()
		{
			return 1f;
		}

		public bool Interact(Humanoid user, bool hold, bool alt)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (hold || (Object)(object)user != (Object)(object)Player.m_localPlayer)
			{
				return false;
			}
			if (ZNet.instance.IsServer())
			{
				Plugin.Instance.ClaimHome(((Component)this).transform.position);
			}
			else
			{
				Plugin.Instance.RequestHome();
			}
			return true;
		}

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

		private void OnDestroy()
		{
			homes.Remove(this);
			foreach (Mesh mesh in meshes)
			{
				if ((Object)(object)mesh != (Object)null)
				{
					Object.Destroy((Object)(object)mesh);
				}
			}
			foreach (Material material in materials)
			{
				if ((Object)(object)material != (Object)null)
				{
					Object.Destroy((Object)(object)material);
				}
			}
		}
	}
	internal sealed class Companion : MonoBehaviour
	{
		private Plugin plugin;

		private SaveState state;

		private Transform model;

		private Model modelAnimator;

		private MapPin pin;

		private LudvigAudio audio;

		private float voiceEnvironmentTimer = 35f;

		private bool voiceStarted;

		private bool voicePlayerDead;

		private bool voiceHadThreat;

		private float phaseTimer;

		private float dialogueTimer;

		private float retryTimer;

		private float chopTimer;

		private float stuckTimer;

		private float enemyScanTimer;

		private float fleeTimer;

		private float ambienceTimer;

		private float woodApproachTimer;

		private float woodNearestDistance = float.MaxValue;

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

		private float combatTimer;

		private float combatHealth = 55f;

		private float combatAttackTimer;

		private float combatFleeReturnTimer;

		private bool combatActive;

		private bool combatFled;

		private Light nightLight;

		private bool tempoDriving;

		private Vector3 lastMovePosition;

		private Vector3 fleeTarget;

		private int lastDialogue = -1;

		private readonly string[] chatter = new string[20]
		{
			"Den busken flyttet på seg. Jeg velger å kalle det vind.", "Du har bygget et fint hus. Har du vurdert en nødutgang til nødutgangen?", "Jeg er ikke redd for mørket. Bare det som har valgt å bo der.", "En øks er et nyttig verktøy. Særlig hvis du slipper å bruke den.", "Du sier det er en fredelig eng. Har du spurt biene?", "Jeg har spart på kreftene hele dagen. Nå har jeg nesten for mange.", "Var det en kvist som knakk? Jeg har et ganske anstrengt forhold til kvister.", "Jeg liker bålet. Det er det eneste her som freser uten å følge etter meg.", "Et troll bruker også skogen. Forskjellen er at skogen merker det.", "Vikingene sier de frykter ingenting. Det må være slitsomt å huske så lite.",
			"Jeg ville tatt snarveien, men den har for mange muligheter.", "En seng ved veggen, takk. Da kan bare halvparten av verden overraske meg.", "Du kan kalle det eventyr. Jeg fører det som uforutsette utgifter.", "Det er stille her. Litt for stille til at jeg tør å kommentere det.", "Hvis denne planen fungerer, foreslår jeg at vi slutter mens vi ligger foran.", "Jeg tok med niste. Så tok jeg en pause. Nå har jeg bare pausen igjen.", "Et gjerde er fint. Et gjerde til er enda finere.", "Jeg løper ikke fra problemer. Jeg flytter bare møtestedet.", "Du skal bygge en båt? Jeg har alltid ment fast grunn er undervurdert.", "Et hjem er der du vet hvem som lager lydene om natten."
		};

		private readonly string[] events = new string[4] { "Det var bare stein og stillhet der ute. Jeg stolte mest på steinen.", "Jeg fant en ekstra åre! Først trodde jeg det var en felle. Det kan det fortsatt være.", "Et greydverg stilte seg i veien. Jeg tok en omvei med svært grundig sikkerhetsmargin.", "Jeg fant en forlatt sekk. Ingen eier, ingen tenner. En sjelden kombinasjon." };

		internal bool CombatBusy
		{
			get
			{
				if (!combatActive)
				{
					return combatFled;
				}
				return true;
			}
		}

		internal void Init(Plugin owner, SaveState data)
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			plugin = owner;
			state = data;
			model = Model.Build(((Component)this).transform, plugin);
			modelAnimator = ((Component)model).GetComponent<Model>();
			pin = new MapPin(plugin);
			audio = ((Component)this).gameObject.AddComponent<LudvigAudio>();
			audio.Init(plugin);
			CreateNightLight();
			SetVisible(state.job.phase != JobPhase.Away);
			lastMovePosition = ((Component)this).transform.position;
			dialogueTimer = Random.Range(8f, 16f);
		}

		private void CreateNightLight()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("Ludvig_NightLantern");
			val.transform.SetParent(((Component)this).transform, false);
			val.transform.localPosition = new Vector3(0.32f, 1.18f, 0.16f);
			nightLight = val.AddComponent<Light>();
			nightLight.type = (LightType)2;
			nightLight.range = 11f;
			nightLight.intensity = 1.45f;
			nightLight.color = new Color(1f, 0.68f, 0.32f);
			nightLight.shadows = (LightShadows)2;
			((Behaviour)nightLight).enabled = false;
		}

		private bool IsNight()
		{
			try
			{
				if ((Object)(object)EnvMan.instance != (Object)null)
				{
					MethodInfo method = typeof(EnvMan).GetMethod("GetDayFraction", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (method != null)
					{
						float num = Convert.ToSingle(method.Invoke(EnvMan.instance, null));
						return num < 0.2f || num > 0.8f;
					}
				}
			}
			catch
			{
			}
			return RenderSettings.ambientIntensity < 0.55f;
		}

		private void UpdateNightLight(float dt)
		{
			bool flag = IsNight();
			if ((Object)(object)nightLight != (Object)null)
			{
				((Behaviour)nightLight).enabled = flag && state.recruited && !tempoDriving;
			}
			ambienceTimer -= dt;
			if (!state.silent && flag && ambienceTimer <= 0f && state.recruited && state.job.phase == JobPhase.Idle && (Object)(object)audio != (Object)null && !audio.IsSpeaking)
			{
				ambienceTimer = Random.Range(75f, 130f);
				Voice("night");
			}
		}

		internal void GoHome()
		{
			if (state.job.phase != JobPhase.Idle || state.woodPaid || state.woodCargo > 0)
			{
				plugin.Say("Jeg gjør ferdig jobben først.");
				return;
			}
			if (!state.hasBuiltHome)
			{
				plugin.Say("Bygg og velg hytta mi først.");
				Voice("home_missing");
				return;
			}
			state.goingHome = true;
			state.recruited = true;
			state.waiting = false;
			Voice("home_walk");
			plugin.Save();
		}

		internal void StartOre(string prefab, string name, int coins, int capacity, bool prepaid = false)
		{
			//IL_0145: 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_0155: 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_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: 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_01b8: 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_0217: Unknown result type (might be due to invalid IL or missing references)
			//IL_0218: Unknown result type (might be due to invalid IL or missing references)
			//IL_021d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_0229: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0253: Unknown result type (might be due to invalid IL or missing references)
			//IL_0268: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: 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_0347: Unknown result type (might be due to invalid IL or missing references)
			//IL_0348: Unknown result type (might be due to invalid IL or missing references)
			if (state.job.phase != JobPhase.Idle)
			{
				return;
			}
			if (!plugin.FreeTests && Time.time < state.oreCooldownUntil)
			{
				plugin.Say("Jeg må hvile før neste malmtur. Prøv igjen om " + Mathf.CeilToInt(state.oreCooldownUntil - Time.time) + " sekunder.");
				return;
			}
			if (!plugin.ProgressionAllows(prefab))
			{
				plugin.Say("Vi har ikke kommet langt nok i verden til at jeg kan skaffe " + name + " ennå.");
				return;
			}
			if (state.woodPaid || state.woodCargo > 0)
			{
				plugin.Say("Jeg har fortsatt en betalt vedjobb. Ett bekymringsområde av gangen.");
				return;
			}
			if ((Object)(object)ObjectDB.instance == (Object)null || (Object)(object)ObjectDB.instance.GetItemPrefab(prefab) == (Object)null)
			{
				plugin.Say("Den malmtypen finnes ikke i denne spillversjonen. Ingen betaling trukket.");
				return;
			}
			if (!plugin.Save())
			{
				plugin.Say("Jeg får ikke lagret oppdraget. Vi prøver igjen senere.");
				return;
			}
			if (!prepaid && !GameApi.SpendCoins(coins))
			{
				plugin.Say("Du har ikke nok mynter. Jeg tar betalt før jeg går meg vill.");
				Voice("pay_none");
				return;
			}
			state.departure = ((Component)this).transform.position;
			Vector3 val = ((Component)this).transform.position - plugin.RequesterPosition(state.requesterPeer, ((Component)this).transform.position + ((Component)this).transform.forward);
			Vector3 val2 = ((Vector3)(ref val)).normalized;
			if (((Vector3)(ref val2)).sqrMagnitude < 0.1f)
			{
				val2 = -((Component)this).transform.forward;
			}
			Vector3 point = ((Component)this).transform.position + val2 * Mathf.Clamp(plugin.TravelDistance.Value, 8f, 60f);
			bool flag = false;
			for (int i = 0; i < 8; i++)
			{
				if (Plugin.Ground(ref point))
				{
					flag = true;
					break;
				}
				val2 = Quaternion.Euler(0f, 45f, 0f) * val2;
				point = ((Component)this).transform.position + val2 * Mathf.Clamp(plugin.TravelDistance.Value, 8f, 60f);
			}
			if (!flag)
			{
				point = ((Component)this).transform.position;
			}
			float num;
			switch (prefab)
			{
			default:
				num = 4.5f;
				break;
			case "BlackMetalScrap":
				num = 3.5f;
				break;
			case "SilverOre":
				num = 2.7f;
				break;
			case "IronScrap":
				num = 1.8f;
				break;
			case "CopperOre":
			case "TinOre":
				num = 1f;
				break;
			}
			float num2 = num;
			state.job = Expedition.Start(prefab, name, plugin.JobSeconds * num2, capacity, Random.Range(0, 4), state.waiting);
			state.job.cargo = ServiceRules.OreReward(capacity, state.job.eventId);
			state.position = ((Component)this).transform.position;
			state.woodTarget = point;
			phaseTimer = 0f;
			state.oreCooldownUntil = (plugin.FreeTests ? 0f : (Time.time + plugin.OreCooldown.Value * num2));
			plugin.Say("Da er vi enige om " + coins + " mynter. Jeg går etter " + name + ". Hvis noen spør, var dette din idé.");
			if ((Object)(object)audio != (Object)null)
			{
				switch (prefab)
				{
				case "IronScrap":
					Voice("ore_iron");
					break;
				case "CopperOre":
					Voice("ore_copper");
					break;
				case "TinOre":
					Voice("ore_tin");
					break;
				case "SilverOre":
					Voice("ore_silver");
					break;
				case "BlackMetalScrap":
					Voice("ore_blackmetal");
					break;
				case "FlametalNew":
				case "FlametalOreNew":
					Voice("ore_flametal");
					break;
				}
			}
			plugin.Save();
		}

		internal void Tick(Player player, float dt)
		{
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_026e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0204: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_030d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0318: Unknown result type (might be due to invalid IL or missing references)
			TickVoiceEnvironment(player, dt);
			if ((Object)(object)audio != (Object)null && !state.silent)
			{
				audio.TickEnemy(dt);
			}
			UpdateNightLight(dt);
			if ((Object)(object)modelAnimator != (Object)null)
			{
				modelAnimator.Animate(0f, (Object)(object)audio != (Object)null && audio.IsSpeaking, Time.time, tempoDriving);
			}
			if (state.job.phase != JobPhase.Idle)
			{
				TickJob(player, dt);
			}
			else if (state.goingHome)
			{
				if (Move(state.home, dt))
				{
					state.goingHome = false;
					state.waiting = true;
					Voice("home_arrive");
					plugin.Save();
				}
			}
			else if (combatFled)
			{
				TickCombatReturn(dt);
			}
			else if (combatActive)
			{
				TickCombat(player, dt);
			}
			else
			{
				if (state.recruited && TickDanger(player, dt))
				{
					return;
				}
				SetVisible(visible: true);
				if (!state.recruited)
				{
					return;
				}
				if (state.woodCargo > 0)
				{
					retryTimer -= dt;
					if (retryTimer <= 0f && plugin.RequesterInRange(((Component)this).transform.position, 6f))
					{
						retryTimer = 5f;
						state.woodCargo -= plugin.DeliverToRequester("Wood", state.woodCargo, "wood");
						plugin.Save();
						if (state.woodCargo == 0)
						{
							plugin.Say("Veden er levert. Jeg har fortsatt flis i selvfølelsen.");
						}
					}
					Vector3 val = plugin.RequesterPosition(state.requesterPeer, plugin.FollowPosition(((Component)player).transform.position));
					if (!state.waiting && Vector3.Distance(((Component)this).transform.position, val) > 4f)
					{
						Move(val, dt);
					}
					return;
				}
				if (state.woodPaid)
				{
					TickWood(dt);
					return;
				}
				Vector3 val2 = plugin.FollowPosition(((Component)player).transform.position);
				if (!state.waiting && Vector3.Distance(((Component)this).transform.position, val2) > plugin.FollowDistance.Value)
				{
					Move(val2, dt);
				}
				dialogueTimer -= dt;
				if (!(dialogueTimer <= 0f))
				{
					return;
				}
				dialogueTimer = Random.Range(12f, 24f);
				int num;
				do
				{
					num = Random.Range(0, chatter.Length);
				}
				while (num == lastDialogue);
				lastDialogue = num;
				if (!state.silent && Vector3.Distance(((Component)this).transform.position, ((Component)player).transform.position) < 25f)
				{
					plugin.Say(chatter[num]);
					if ((Object)(object)audio != (Object)null)
					{
						Voice("idle");
					}
				}
			}
		}

		private void TickJob(Player player, float dt)
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0218: Unknown result type (might be due to invalid IL or missing references)
			//IL_021d: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0231: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_023f: Unknown result type (might be due to invalid IL or missing references)
			Expedition job = state.job;
			phaseTimer += dt;
			switch (job.phase)
			{
			case JobPhase.Leaving:
				if (Move(state.woodTarget, dt) || phaseTimer > 12f)
				{
					job.phase = JobPhase.Away;
					phaseTimer = 0f;
					SetVisible(visible: false);
					plugin.Save();
				}
				break;
			case JobPhase.Away:
				SetVisible(visible: false);
				if (!job.eventReported && job.remaining < Math.Max(10f, plugin.Duration.Value) * 0.6f)
				{
					job.eventReported = true;
					plugin.Say("Rapport fra turen: " + events[job.eventId]);
					plugin.Save();
				}
				if (job.AdvanceAway(dt))
				{
					SetVisible(visible: true);
					phaseTimer = 0f;
					plugin.Save();
				}
				break;
			case JobPhase.Returning:
			{
				SetVisible(visible: true);
				Vector3 destination = plugin.RequesterPosition(state.requesterPeer, state.departure);
				if (Move(destination, dt) || phaseTimer > 30f)
				{
					if (phaseTimer > 30f)
					{
						((Component)this).transform.position = state.departure;
					}
					state.tempoAdventure = false;
					job.phase = JobPhase.Delivery;
					phaseTimer = 0f;
					Voice("return");
					plugin.Say("Tilbake med " + job.cargo + " " + job.displayName + ". Jeg vil ikke snakke om busken på veien.");
					plugin.Save();
				}
				break;
			}
			case JobPhase.Delivery:
			{
				Vector3 val = plugin.RequesterPosition(state.requesterPeer, ((Component)player).transform.position);
				if (!job.resumeWaiting && Vector3.Distance(((Component)this).transform.position, val) > 4f)
				{
					Move(val, dt);
				}
				retryTimer -= dt;
				if (!(retryTimer <= 0f) || !plugin.RequesterInRange(((Component)this).transform.position, 6f))
				{
					break;
				}
				retryTimer = 5f;
				int num = plugin.DeliverToRequester(job.ore, job.cargo, "primary");
				job.AcknowledgeDelivery(num);
				plugin.Save();
				if (job.phase == JobPhase.Idle)
				{
					if (!string.IsNullOrEmpty(state.huntSecondaryPrefab) && state.huntSecondaryCargo > 0)
					{
						int num2 = plugin.DeliverToRequester(state.huntSecondaryPrefab, state.huntSecondaryCargo, "secondary");
						state.huntSecondaryCargo -= num2;
						if (state.huntSecondaryCargo > 0)
						{
							job.phase = JobPhase.Delivery;
							plugin.Say("Jeg har også " + state.huntSecondaryCargo + " ekstra jaktvarer igjen. Lag plass.");
							plugin.Save();
							break;
						}
						state.huntSecondaryPrefab = "";
						state.huntAnimal = "";
					}
					state.waiting = job.resumeWaiting;
					plugin.Say("Alt levert. Nå tar jeg en pause med faglig begrunnelse.");
					plugin.Save();
				}
				else if (num > 0 || phaseTimer < 6f)
				{
					plugin.Say("Jeg beholder " + job.cargo + " " + job.displayName + " til du har plass. Kom nær meg igjen.");
				}
				break;
			}
			}
		}

		internal void StartWood(Player player, int coins, int capacity, bool prepaid = false)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			if (state.job.phase != JobPhase.Idle || state.woodCargo > 0)
			{
				return;
			}
			if (state.woodPaid)
			{
				plugin.Say("Du har allerede betalt. Treet husker det nok også.");
				return;
			}
			TreeBase val = FindTree(((Component)player).transform.position, plugin.TreeSearchRadius.Value);
			if ((Object)(object)val == (Object)null)
			{
				val = FindTree(((Component)player).transform.position, Mathf.Max(plugin.TreeSearchRadius.Value, 120f));
			}
			if ((Object)(object)val == (Object)null)
			{
				plugin.Say("Jeg finner ingen trær her. Ingen betaling trukket.");
				return;
			}
			if (!plugin.Save())
			{
				plugin.Say("Lagringen svikter. Vi prøver igjen senere.");
				return;
			}
			if (!prepaid && !GameApi.SpendCoins(coins))
			{
				plugin.Say("Du har ikke nok mynter til den vedjobben.");
				Voice("pay_none");
				return;
			}
			state.woodPaid = true;
			state.woodHits = 0;
			state.woodCargo = 0;
			state.woodCollected = 0;
			state.woodCapacity = capacity;
			state.woodTarget = ((Component)val).transform.position;
			woodApproachTimer = 0f;
			woodNearestDistance = float.MaxValue;
			rejectedWoodTargets.Clear();
			plugin.Say("Da var betalingen i orden. Jeg hogger det treet. Du tar imot hvis det blir veldig engasjert.");
			if ((Object)(object)audio != (Object)null)
			{
				Voice("wood");
			}
			plugin.Save();
		}

		private unsafe void TickWood(float dt)
		{
			//IL_003b: 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_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: 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_00ed: 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_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0330: Unknown result type (might be due to invalid IL or missing references)
			//IL_0258: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Expected O, but got Unknown
			//IL_027a: Unknown result type (might be due to invalid IL or missing references)
			//IL_027b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0280: Unknown result type (might be due to invalid IL or missing references)
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_028c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0293: Unknown result type (might be due to invalid IL or missing references)
			//IL_0298: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0513: Unknown result type (might be due to invalid IL or missing references)
			//IL_0546: Unknown result type (might be due to invalid IL or missing references)
			//IL_054b: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_03da: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ec: Unknown result type (might be due to invalid IL or missing references)
			chopTimer -= dt;
			if (state.RemainingWood == 0)
			{
				state.woodPaid = false;
				plugin.Save();
				return;
			}
			TreeBase val = FindTree(state.woodTarget, 1.5f);
			TreeLog val2 = null;
			Collider[] array;
			if ((Object)(object)val == (Object)null)
			{
				float num = 10f;
				array = Physics.OverlapSphere(state.woodTarget, 10f);
				for (int i = 0; i < array.Length; i++)
				{
					TreeLog componentInParent = ((Component)array[i]).GetComponentInParent<TreeLog>();
					if (!((Object)(object)componentInParent == (Object)null))
					{
						float num2 = Vector3.Distance(state.woodTarget, ((Component)componentInParent).transform.position);
						if (num2 < num)
						{
							num = num2;
							val2 = componentInParent;
						}
					}
				}
			}
			Vector3 val3 = (((Object)(object)val != (Object)null) ? ((Component)val).transform.position : (((Object)(object)val2 != (Object)null) ? ((Component)val2).transform.position : state.woodTarget));
			Vector3 point = val3;
			float num3 = Vector3.Distance(((Component)this).transform.position, val3);
			if (num3 < woodNearestDistance - 0.25f)
			{
				woodNearestDistance = num3;
				woodApproachTimer = 0f;
			}
			else
			{
				woodApproachTimer += dt;
			}
			Vector3 val4;
			if (((Object)(object)val != (Object)null || (Object)(object)val2 != (Object)null) && (!Plugin.Ground(ref point) || woodApproachTimer > 20f))
			{
				rejectedWoodTargets.Add(state.woodTarget);
				val = null;
				val2 = null;
				state.woodHits = 40;
				chopTimer = -4f;
				plugin.Say("Den veden kan jeg ikke nå trygt. Jeg leter etter et annet tre på land.");
				Voice("wood_water");
				Plugin obj = plugin;
				val4 = val3;
				obj.LogInfo("Wood recovery: abandoned wet or unreachable target at " + ((object)(*(Vector3*)(&val4))/*cast due to .constrained prefix*/).ToString());
			}
			if ((Object)(object)val != (Object)null || (Object)(object)val2 != (Object)null)
			{
				if (Vector3.Distance(((Component)this).transform.position, val3) > 2.5f)
				{
					Move(val3, dt);
					return;
				}
				if (chopTimer > 0f)
				{
					return;
				}
				chopTimer = 1.5f;
				if ((Object)(object)modelAnimator != (Object)null)
				{
					modelAnimator.Gesture("Chop", 1.4f);
				}
				HitData val5 = new HitData();
				val5.m_damage.m_chop = 35f;
				val5.m_toolTier = 4;
				val5.m_point = val3 + Vector3.up;
				val4 = val3 - ((Component)this).transform.position;
				val5.m_dir = ((Vector3)(ref val4)).normalized;
				if ((Object)(object)val != (Object)null)
				{
					val.Damage(val5);
				}
				else
				{
					val2.Damage(val5);
				}
				state.woodHits++;
				if (state.woodHits < 40)
				{
					plugin.Save();
					return;
				}
			}
			if (chopTimer > -3f && state.woodHits < 40)
			{
				return;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Wood");
			HashSet<ItemDrop> hashSet = new HashSet<ItemDrop>();
			array = Physics.OverlapSphere(state.woodTarget, 10f);
			for (int i = 0; i < array.Length; i++)
			{
				ItemDrop componentInParent2 = ((Component)array[i]).GetComponentInParent<ItemDrop>();
				if ((Object)(object)componentInParent2 == (Object)null || !hashSet.Add(componentInParent2) || (Object)(object)itemPrefab == (Object)null || componentInParent2.m_itemData.m_shared.m_name != itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared.m_name)
				{
					continue;
				}
				Vector3 point2 = ((Component)componentInParent2).transform.position;
				if (!Plugin.Ground(ref point2))
				{
					continue;
				}
				if (Vector3.Distance(((Component)this).transform.position, ((Component)componentInParent2).transform.position) > 4f)
				{
					Move(point2, dt);
					return;
				}
				int remainingWood = state.RemainingWood;
				if (remainingWood <= 0)
				{
					break;
				}
				int num4 = Mathf.Min(remainingWood, componentInParent2.m_itemData.m_stack);
				ZNetView component = ((Component)componentInParent2).GetComponent<ZNetView>();
				if ((Object)(object)component == (Object)null || !component.IsValid())
				{
					continue;
				}
				if (!component.IsOwner())
				{
					component.ClaimOwnership();
				}
				if (component.IsOwner())
				{
					state.RecordWoodTake(num4);
					if (num4 >= componentInParent2.m_itemData.m_stack)
					{
						ZNetScene.instance.Destroy(((Component)componentInParent2).gameObject);
						continue;
					}
					ItemData itemData = componentInParent2.m_itemData;
					itemData.m_stack -= num4;
					AccessTools.Method(typeof(ItemDrop), "Save", (Type[])null, (Type[])null).Invoke(componentInParent2, null);
				}
			}
			if (state.woodCargo > state.woodCapacity)
			{
				state.woodCargo = state.woodCapacity;
			}
			if (state.RemainingWood > 0)
			{
				TreeBase val6 = FindTree(((Component)this).transform.position, plugin.TreeSearchRadius.Value);
				if ((Object)(object)val6 != (Object)null)
				{
					state.woodTarget = ((Component)val6).transform.position;
					state.woodHits = 0;
					chopTimer = 0f;
					woodApproachTimer = 0f;
					woodNearestDistance = float.MaxValue;
					plugin.Say("Jeg har plass til mer ved. Jeg finner et nytt tre.");
					plugin.Save();
					return;
				}
			}
			state.woodPaid = false;
			if ((Object)(object)audio != (Object)null)
			{
				Voice("wood_done");
			}
			plugin.Say((state.woodCargo > 0) ? ("Sekken er full nok. Jeg har " + state.woodCargo + " ved. Kom bort, så leverer jeg.") : "Vedjobben er over. Jeg fant ikke mer som kunne samles inn.");
			plugin.Save();
		}

		internal void StartHunt(string animal, string primaryPrefab, string secondaryPrefab, int coins, int capacity, bool prepaid = false)
		{
			//IL_0134: 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_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: 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)
			//IL_01a6: 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_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: 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_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_0231: Unknown result type (might be due to invalid IL or missing references)
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			if (state.job.phase != JobPhase.Idle || state.woodPaid || state.woodCargo > 0)
			{
				plugin.Say("Jeg må gjøre ferdig den andre jobben først.");
				return;
			}
			if (!plugin.ProgressionAllows(primaryPrefab))
			{
				plugin.Say("Dette jaktoppdraget er ikke låst opp ennå.");
				return;
			}
			if ((Object)(object)ObjectDB.instance == (Object)null || (Object)(object)ObjectDB.instance.GetItemPrefab(primaryPrefab) == (Object)null)
			{
				plugin.Say("Jeg finner ikke riktig jaktutbytte i denne spillversjonen. Ingen betaling trukket.");
				return;
			}
			if (!plugin.Save())
			{
				plugin.Say("Jeg får ikke lagret jaktoppdraget. Ingen betaling trukket.");
				return;
			}
			if (!prepaid && !GameApi.SpendCoins(coins))
			{
				plugin.Say("Du har ikke nok mynter til den jaktturen.");
				Voice("pay_none");
				return;
			}
			bool flag = ServiceRules.IsTrophy(primaryPrefab);
			state.huntSecondaryPrefab = (flag ? "" : secondaryPrefab);
			state.huntSecondaryCargo = ((!string.IsNullOrEmpty(state.huntSecondaryPrefab)) ? Mathf.Max(1, capacity / 2) : 0);
			state.huntAnimal = animal;
			state.departure = ((Component)this).transform.position;
			Vector3 val = ((Component)this).transform.position - plugin.RequesterPosition(state.requesterPeer, ((Component)this).transform.position + ((Component)this).transform.forward);
			Vector3 val2 = ((Vector3)(ref val)).normalized;
			if (((Vector3)(ref val2)).sqrMagnitude < 0.1f)
			{
				val2 = -((Component)this).transform.forward;
			}
			Vector3 point = ((Component)this).transform.position + val2 * Mathf.Clamp(plugin.TravelDistance.Value, 12f, 70f);
			for (int i = 0; i < 8; i++)
			{
				if (Plugin.Ground(ref point))
				{
					break;
				}
				val2 = Quaternion.Euler(0f, 45f, 0f) * val2;
				point = ((Component)this).transform.position + val2 * Mathf.Clamp(plugin.TravelDistance.Value, 12f, 70f);
			}
			state.woodTarget = point;
			state.job = Expedition.Start(primaryPrefab, animal, plugin.JobSeconds, capacity, Random.Range(0, 4), state.waiting);
			if (flag)
			{
				state.job.cargo = 1;
				state.job.remaining = (plugin.FreeTests ? plugin.JobSeconds : Mathf.Max(180f, state.job.remaining));
				Voice("trophy_start");
			}
			phaseTimer = 0f;
			plugin.Say("Betaling mottatt. Jeg drar på jakt etter " + animal + ". Jeg foretrekker dyr som ikke jakter tilbake.");
			if ((Object)(object)audio != (Object)null)
			{
				switch (animal)
				{
				case "hjort":
					Voice("hunt_deer");
					break;
				case "villsvin":
					Voice("hunt_boar");
					break;
				case "neck":
					Voice("hunt_neck");
					break;
				default:
					switch (primaryPrefab)
					{
					case "WolfMeat":
						Voice("hunt_wolf");
						break;
					case "LoxMeat":
						Voice("hunt_lox");
						break;
					case "HareMeat":
						Voice("hunt_hare");
						break;
					default:
						Voice("hunt_start");
						break;
					}
					break;
				}
			}
			plugin.Save();
		}

		private TreeBase FindTree(Vector3 point, float radius)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: 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)
			TreeBase result = null;
			float num = radius;
			Collider[] array = Physics.OverlapSphere(point, radius);
			for (int i = 0; i < array.Length; i++)
			{
				TreeBase componentInParent = ((Component)array[i]).GetComponentInParent<TreeBase>();
				if ((Object)(object)componentInParent == (Object)null)
				{
					continue;
				}
				Vector3 point2 = ((Component)componentInParent).transform.position;
				if (!Plugin.Ground(ref point2))
				{
					continue;
				}
				bool flag = false;
				foreach (Vector3 rejectedWoodTarget in rejectedWoodTargets)
				{
					if (Vector3.Distance(rejectedWoodTarget, ((Component)componentInParent).transform.position) < 2f)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					float num2 = Vector3.Distance(point, ((Component)componentInParent).transform.position);
					if (num2 < num)
					{
						result = componentInParent;
						num = num2;
					}
				}
			}
			return result;
		}

		internal void CancelWork()
		{
			combatActive = false;
			combatFled = false;
			combatTimer = 0f;
			combatFleeReturnTimer = 0f;
			fleeTimer = 0f;
			phaseTimer = 0f;
			retryTimer = 0f;
			chopTimer = 0f;
			SetVisible(visible: true);
		}

		internal void StartFight(int coins, float seconds, bool prepaid = false)
		{
			if (state.job.phase != JobPhase.Idle || state.woodPaid || state.woodCargo > 0)
			{
				plugin.Say("Jeg må gjøre ferdig jobben først.");
				return;
			}
			if (combatActive || combatFled)
			{
				plugin.Say(combatFled ? "Jeg gjemmer meg fortsatt. Ti minutter er ti minutter." : "Jeg slåss allerede så godt jeg kan.");
				return;
			}
			if (!prepaid && !GameApi.SpendCoins(coins))
			{
				plugin.Say("Du har ikke nok mynter til livvakttjenesten.");
				Voice("pay_none");
				return;
			}
			combatActive = true;
			combatTimer = Mathf.Max(60f, seconds);
			combatHealth = 55f;
			combatAttackTimer = 0f;
			state.waiting = false;
			state.recruited = true;
			state.goingHome = false;
			fleeTimer = 0f;
			plugin.Say("Jaja... jeg blir med og slåss. Men hvis dette blir farlig, så løper jeg først.");
			Voice("follow");
			plugin.Save();
		}

		private void TickCombat(Player player, float dt)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0305: Unknown result type (might be due to invalid IL or missing references)
			//IL_030a: Unknown result type (might be due to invalid IL or missing references)
			//IL_030f: 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_034b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0355: Unknown result type (might be due to invalid IL or missing references)
			//IL_035a: Unknown result type (might be due to invalid IL or missing references)
			//IL_035f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0331: Unknown result type (might be due to invalid IL or missing references)
			//IL_0336: Unknown result type (might be due to invalid IL or missing references)
			//IL_033b: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Expected O, but got Unknown
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: 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_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_0254: Unknown result type (might be due to invalid IL or missing references)
			//IL_0259: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: 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)
			Player player2 = Player.GetPlayer(state.requesterPlayerId);
			if ((Object)(object)player2 != (Object)null)
			{
				player = player2;
			}
			SetVisible(visible: true);
			combatTimer -= dt;
			combatAttackTimer -= dt;
			if (combatTimer <= 0f)
			{
				combatActive = false;
				plugin.Say("Da var vakta mi over. Jeg har brukt opp motet for i dag.");
				return;
			}
			Character val = null;
			float num = 22f;
			Collider[] array = Physics.OverlapSphere(((Component)player).transform.position, 20f);
			foreach (Collider val2 in array)
			{
				Character val3 = (((Object)(object)val2 == (Object)null) ? null : ((Component)val2).GetComponentInParent<Character>());
				if (!((Object)(object)val3 == (Object)null) && !((Object)(object)val3 == (Object)(object)player) && !val3.IsDead() && !(val3 is Player) && BaseAI.IsEnemy((Character)(object)player, val3))
				{
					float num2 = Vector3.Distance(((Component)this).transform.position, ((Component)val3).transform.position);
					if (num2 < num)
					{
						num = num2;
						val = val3;
					}
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				if (voiceHadThreat)
				{
					voiceHadThreat = false;
					Voice("combat_end");
				}
				if (Vector3.Distance(((Component)this).transform.position, ((Component)player).transform.position) > 3.2f)
				{
					Move(((Component)player).transform.position - ((Component)player).transform.forward * 2.2f, dt, 4.6f, allowRecovery: true);
				}
				return;
			}
			voiceHadThreat = true;
			if (num > 2.2f)
			{
				Move(((Component)val).transform.position, dt, 4.8f, allowRecovery: true);
			}
			else
			{
				if (!(combatAttackTimer <= 0f))
				{
					return;
				}
				combatAttackTimer = 1.25f;
				HitData val4 = new HitData();
				val4.m_damage.m_blunt = 15f;
				val4.m_point = ((Component)val).transform.position + Vector3.up;
				Vector3 val5 = ((Component)val).transform.position - ((Component)this).transform.position;
				val4.m_dir = ((Vector3)(ref val5)).normalized;
				val4.SetAttacker((Character)(object)player);
				((Component)this).transform.rotation = Quaternion.LookRotation(new Vector3(val4.m_dir.x, 0f, val4.m_dir.z));
				if ((Object)(object)modelAnimator != (Object)null)
				{
					modelAnimator.Gesture("Chop", 1.1f);
				}
				try
				{
					val.Damage(val4);
					Voice(val.IsDead() ? "enemy_defeated" : "combat_attack");
				}
				catch (Exception ex)
				{
					plugin.LogInfo("Ludvig combat hit failed: " + ex.Message);
				}
				if (combatHealth <= 15f)
				{
					combatActive = false;
					combatFled = true;
					combatFleeReturnTimer = 600f;
					Vector3 val6 = ((Component)this).transform.position - ((Component)val).transform.position;
					val6.y = 0f;
					if (((Vector3)(ref val6)).sqrMagnitude < 0.1f)
					{
						val6 = -((Component)player).transform.forward;
					}
					fleeTarget = ((Component)this).transform.position + ((Vector3)(ref val6)).normalized * 20f;
					Plugin.Ground(ref fleeTarget);
					plugin.Say("Å nei, nei, nei! Nå holder det! Jeg gjemmer meg og kommer tilbake senere!");
					Voice("enemy");
				}
			}
		}

		private void TickCombatReturn(float dt)
		{
			//IL_0010: 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_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			combatFleeReturnTimer -= dt;
			Move(fleeTarget, dt, 6.2f, allowRecovery: false);
			if (combatFleeReturnTimer > 0f)
			{
				if (Vector3.Distance(((Component)this).transform.position, fleeTarget) < 2f)
				{
					SetVisible(visible: false);
				}
				return;
			}
			combatFled = false;
			combatHealth = 55f;
			SetVisible(visible: true);
			Vector3 point = state.home;
			if (Plugin.Ground(ref point))
			{
				((Component)this).transform.position = point;
			}
			plugin.Say("Jaja... nå har jeg samlet litt mot igjen. Litt.");
			Voice("enemy_safe");
		}

		private bool TickDanger(Player player, float dt)
		{
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: 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_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			enemyScanTimer -= dt;
			if (fleeTimer <= 0f && enemyScanTimer <= 0f)
			{
				enemyScanTimer = 0.6f;
				Transform val = null;
				float num = 10f;
				Collider[] array = Physics.OverlapSphere(((Component)this).transform.position, 9f);
				foreach (Collider val2 in array)
				{
					if ((Object)(object)val2 == (Object)null)
					{
						continue;
					}
					string text = ((Object)((Component)val2).transform.root).name.ToLowerInvariant();
					if (text.Contains("greyling") || text.Contains("greydwarf") || text.Contains("troll") || text.Contains("skeleton"))
					{
						float num2 = Vector3.Distance(((Component)this).transform.position, ((Component)val2).transform.position);
						if (num2 < num)
						{
							num = num2;
							val = ((Component)val2).transform.root;
						}
					}
				}
				if ((Object)(object)val != (Object)null)
				{
					Vector3 val3 = ((Component)this).transform.position - val.position;
					val3.y = 0f;
					if (((Vector3)(ref val3)).sqrMagnitude < 0.1f)
					{
						val3 = -((Component)player).transform.forward;
					}
					fleeTarget = ((Component)this).transform.position + ((Vector3)(ref val3)).normalized * 12f;
					if (!Plugin.Ground(ref fleeTarget))
					{
						fleeTarget = ((Component)player).transform.position;
					}
					fleeTimer = 5f;
					if ((Object)(object)audio != (Object)null)
					{
						Voice("enemy");
					}
					plugin.Say("Å nei! Jeg løper! Du får ta den der!");
				}
			}
			if (fleeTimer > 0f)
			{
				fleeTimer -= dt;
				Move(fleeTarget, dt, 6f, allowRecovery: false);
				return true;
			}
			return false;
		}

		private bool Move(Vector3 destination, float dt)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			float num = Vector3.Distance(((Component)this).transform.position, destination);
			return Move(destination, dt, (num > 10f) ? 3.2f : 1.65f, allowRecovery: true);
		}

		private unsafe bool Move(Vector3 destination, float dt, float speed, bool allowRecovery)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//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_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: 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_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: 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_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: 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_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: 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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0297: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0201: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: Unknown result type (might be due to invalid IL or missing references)
			//IL_0230: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: 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_02fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0309: Unknown result type (might be due to invalid IL or missing references)
			//IL_030e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0313: Unknown result type (might be due to invalid IL or missing references)
			//IL_0324: Unknown result type (might be due to invalid IL or missing references)
			//IL_032c: Unknown result type (might be due to invalid IL or missing references)
			//IL_032e: 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_034b: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = destination - ((Component)this).transform.position;
			val.y = 0f;
			if (((Vector3)(ref val)).magnitude < 1.5f)
			{
				stuckTimer = 0f;
				lastMovePosition = ((Component)this).transform.position;
				if ((Object)(object)model != (Object)null)
				{
					model.localPosition = Vector3.zero;
					model.localRotation = Quaternion.identity;
				}
				return true;
			}
			Vector3 val2 = ((Vector3)(ref val)).normalized;
			Vector3 val3 = ((Component)this).transform.position + Vector3.up * 0.85f;
			float num = Mathf.Min(((Vector3)(ref val)).magnitude, dt * speed);
			RaycastHit val4 = default(RaycastHit);
			if (Physics.SphereCast(val3, 0.28f, val2, ref val4, Mathf.Max(0.45f, num + 0.3f), -1, (QueryTriggerInteraction)1))
			{
				Vector3 val5 = Quaternion.Euler(0f, -48f, 0f) * val2;
				Vector3 val6 = Quaternion.Euler(0f, 48f, 0f) * val2;
				bool flag = !Physics.SphereCast(val3, 0.28f, val5, ref val4, 0.85f, -1, (QueryTriggerInteraction)1);
				bool flag2 = !Physics.SphereCast(val3, 0.28f, val6, ref val4, 0.85f, -1, (QueryTriggerInteraction)1);
				if (flag || flag2)
				{
					val2 = ((flag && !flag2) ? val5 : ((flag2 && !flag) ? val6 : ((Vector3.Dot(val5, val) > Vector3.Dot(val6, val)) ? val5 : val6)));
				}
				else
				{
					val2 = Quaternion.Euler(0f, 90f * ((Mathf.Sin(Time.time * 1.7f) >= 0f) ? 1f : (-1f)), 0f) * val2;
					stuckTimer += dt;
				}
			}
			Vector3 point = ((Component)this).transform.position + val2 * num;
			if (Plugin.Ground(ref point))
			{
				((Component)this).transform.rotation = Quaternion.Slerp(((Component)this).transform.rotation, Quaternion.LookRotation(val2), Mathf.Clamp01(dt * 8f));
				((Component)this).transform.position = point;
				model.localPosition = Vector3.zero;
				model.localRotation = Quaternion.identity;
				if ((Object)(object)modelAnimator != (Object)null)
				{
					modelAnimator.Animate(Mathf.Clamp01(speed / 3.2f), (Object)(object)audio != (Object)null && audio.IsSpeaking, Time.time);
				}
			}
			if (Vector3.Distance(((Component)this).transform.position, lastMovePosition) < 0.08f)
			{
				stuckTimer += dt;
			}
			else
			{
				stuckTimer = 0f;
				lastMovePosition = ((Component)this).transform.position;
			}
			if (allowRecovery && stuckTimer > 2.5f && ((Vector3)(ref val)).magnitude > 5f)
			{
				Vector3 point2 = destination - ((Vector3)(ref val)).normalized * 2f;
				if (Plugin.Ground(ref point2))
				{
					((Component)this).transform.position = point2;
					lastMovePosition = point2;
					stuckTimer = 0f;
					Plugin obj = plugin;
					Vector3 val7 = point2;
					obj.LogInfo("Ludvig movement recovery used at " + ((object)(*(Vector3*)(&val7))/*cast due to .constrained prefix*/).ToString());
				}
			}
			return false;
		}

		private static bool VoiceFlag(Player player, string method)
		{
			try
			{
				MethodInfo method2 = ((object)player).GetType().GetMethod(method, Type.EmptyTypes);
				return method2 != null && method2.ReturnType == typeof(bool) && (bool)method2.Invoke(player, null);
			}
			catch
			{
				return false;
			}
		}

		private void TickVoiceEnvironment(Player player, float dt)
		{
			//IL_00c3: 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)
			if ((Object)(object)player == (Object)null || state.silent || (Object)(object)audio == (Object)null)
			{
				return;
			}
			if (!voiceStarted)
			{
				voiceStarted = true;
				Voice("spawn");
			}
			bool flag = ((Character)player).IsDead();
			if (flag && !voicePlayerDead)
			{
				Voice("player_death");
			}
			voicePlayerDead = flag;
			voiceEnvironmentTimer -= dt;
			if (flag || voiceEnvironmentTimer > 0f || !state.recruited || state.job.phase != JobPhase.Idle || combatActive || combatFled || audio.IsSpeaking || Vector3.Distance(((Component)this).transform.position, ((Component)player).transform.position) > 20f)
			{
				return;
			}
			voiceEnvironmentTimer = Random.Range(80f, 140f);
			if (VoiceFlag(player, "IsFreezing") || VoiceFlag(player, "IsCold"))
			{
				Voice("cold");
				return;
			}
			if (VoiceFlag(player, "IsWet") && (Object)(object)EnvMan.instance != (Object)null)
			{
				try
				{
					MethodInfo method = typeof(EnvMan).GetMethod("IsWet", Type.EmptyTypes);
					if (method != null && (bool)method.Invoke(EnvMan.instance, null))
					{
						Voice("rain");
						return;
					}
				}
				catch
				{
				}
			}
			if (VoiceFlag(player, "IsResting"))
			{
				Voice("rest");
				return;
			}
			try
			{
				MethodInfo method2 = ((object)player).GetType().GetMethod("GetBiome", Type.EmptyTypes);
				string text = ((method2 == null) ? "" : Convert.ToString(method2.Invoke(player, null)).ToLowerInvariant());
				switch (text)
				{
				case "meadows":
				case "blackforest":
				case "swamp":
				case "mountain":
				case "plains":
				case "mistlands":
				case "ashlands":
					Voice("biome_" + text);
					break;
				}
			}
			catch
			{
			}
		}

		private void Voice(string group)
		{
			if (group == "greet" && (Object)(object)modelAnimator != (Object)null)
			{
				modelAnimator.Gesture("Wave", 2f);
			}
			if ((Object)(object)audio != (Object)null)
			{
				audio.Play(group);
			}
			plugin.BroadcastVoice(group);
		}

		internal void PlayVoiceLocal(string group)
		{
			if ((Object)(object)audio != (Object)null)
			{
				audio.Play(group);
			}
		}

		private void SetVisible(bool visible)
		{
			if ((Object)(object)model != (Object)null)
			{
				((Component)model).gameObject.SetActive(visible);
			}
		}

		internal void PlayVoice(string group)
		{
			Voice(group);
		}

		internal void Status()
		{
			string text = ((state.job.phase != JobPhase.Idle) ? (state.job.phase.ToString() + ": " + state.job.displayName + ", " + state.job.cargo + " i sekken") : ((!state.recruited) ? "kan rekrutteres med E" : (state.waiting ? "venter ved hjemmet" : "følger deg")));
			plugin.Say(text + (state.woodPaid ? " — vedjobb pågår" : ""));
		}

		internal void UpdatePin()
		{
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			if (pin == null)
			{
				return;
			}
			if (!plugin.ShowMapPin.Value)
			{
				pin.Remove();
				return;
			}
			string text = "Ludvig";
			if (state.job.phase == JobPhase.Away)
			{
				text += " (på tur)";
			}
			if (state.job.phase == JobPhase.Delivery)
			{
				text += " (klar til levering)";
			}
			pin.Update((state.job.phase == JobPhase.Away) ? state.woodTarget : ((Component)this).transform.position, text);
		}

		internal void NetworkState(bool recruited, bool waiting, JobPhase phase)
		{
			state.recruited = recruited;
			state.waiting = waiting;
			state.job.phase = phase;
			SetVisible(phase != JobPhase.Away);
		}

		internal void NetworkTick(Vector3 target, float dt)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: 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_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			Vector3 position = ((Component)this).transform.position;
			if (Vector3.Distance(position, target) > 20f)
			{
				((Component)this).transform.position = target;
			}
			else
			{
				((Component)this).transform.position = Vector3.Lerp(position, target, Mathf.Clamp01(dt * 12f));
			}
			Vector3 val = ((Component)this).transform.position - position;
			val.y = 0f;
			float num = ((dt > 0.0001f) ? (((Vector3)(ref val)).magnitude / dt) : 0f);
			if (((Vector3)(ref val)).sqrMagnitude > 0.0004f)
			{
				((Component)this).transform.rotation = Quaternion.Slerp(((Component)this).transform.rotation, Quaternion.LookRotation(((Vector3)(ref val)).normalized), Mathf.Clamp01(dt * 10f));
			}
			if ((Object)(object)modelAnimator != (Object)null)
			{
				modelAnimator.Animate(Mathf.Clamp01(num / 3.2f), (Object)(object)audio != (Object)null && audio.IsSpeaking, Time.time);
			}
			UpdateNightLight(dt);
		}

		internal void RemovePin()
		{
			if (pin != null)
			{
				pin.Remove();
			}
		}

		private void OnDestroy()
		{
			RemovePin();
			if ((Object)(object)model != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)model).gameObject);
			}
		}
	}
	public enum JobPhase
	{
		Idle,
		Leaving,
		Away,
		Returning,
		Delivery
	}
	[Serializable]
	public sealed class Expedition
	{
		public JobPhase phase;

		public string ore;

		public string displayName;

		public float remaining;

		public int cargo;

		public int eventId;

		public bool eventReported;

		public bool resumeWaiting;

		public static Expedition Start(string ore, string name, float duration, int baseline, int roll, bool waiting)
		{
			if (roll < 0 || roll > 3)
			{
				throw new ArgumentOutOfRangeException("roll");
			}
			baseline = Math.Max(1, Math.Min(100, baseline));
			int[] array = new int[4] { 0, 3, -3, 1 };
			return new Expedition
			{
				phase = JobPhase.Leaving,
				ore = ore,
				displayName = name,
				remaining = Math.Max(10f, duration) + (float)((roll == 2) ? 15 : 0),
				cargo = Math.Max(1, baseline + array[roll]),
				eventId = roll,
				resumeWaiting = waiting
			};
		}

		public bool AdvanceAway(float dt)
		{
			if (phase != JobPhase.Away)
			{
				return false;
			}
			remaining = Math.Max(0f, remaining - Math.Max(0f, dt));
			if (remaining > 0f)
			{
				return false;
			}
			phase = JobPhase.Returning;
			return true;
		}

		public void AcknowledgeDelivery(int count)
		{
			if (phase != JobPhase.Delivery)
			{
				throw new InvalidOperationException("Not ready to deliver");
			}
			if (count < 0 || count > cargo)
			{
				throw new ArgumentOutOfRangeException("count");
			}
			cargo -= count;
			if (cargo == 0)
			{
				phase = JobPhase.Idle;
			}
		}
	}
	internal static class GameApi
	{
		internal static bool SpendCoins(int amount)
		{
			if (amount == 0 || ((Object)(object)Plugin.Instance != (Object)null && Plugin.Instance.FreeTests))
			{
				return true;
			}
			Player localPlayer = Player.m_localPlayer;
			GameObject val = (((Object)(object)ObjectDB.instance == (Object)null) ? null : ObjectDB.instance.GetItemPrefab("Coins"));
			if ((Object)(object)localPlayer == (Object)null || (Object)(object)val == (Object)null)
			{
				return false;
			}
			ItemDrop component = val.GetComponent<ItemDrop>();
			if ((Object)(object)component == (Object)null)
			{
				return false;
			}
			Inventory inventory = ((Humanoid)localPlayer).GetInventory();
			string name = component.m_itemData.m_shared.m_name;
			if (inventory.CountItems(name, -1, true) < amount)
			{
				return false;
			}
			inventory.RemoveItem(name, amount, -1, true);
			return true;
		}

		internal static int Deliver(string prefabName, int amount)
		{
			Player localPlayer = Player.m_localPlayer;
			GameObject val = (((Object)(object)ObjectDB.instance == (Object)null) ? null : ObjectDB.instance.GetItemPrefab(prefabName));
			if ((Object)(object)localPlayer == (Object)null || (Object)(object)val == (Object)null)
			{
				return 0;
			}
			ItemDrop component = val.GetComponent<ItemDrop>();
			if ((Object)(object)component == (Object)null)
			{
				return 0;
			}
			int num = 0;
			for (int i = 0; i < amount; i++)
			{
				ItemData val2 = component.m_itemData.Clone();
				val2.m_dropPrefab = val;
				val2.m_stack = 1;
				if (!((Humanoid)localPlayer).GetInventory().AddItem(val2))
				{
					break;
				}
				num++;
			}
			return num;
		}

		internal static bool InputBlocked()
		{
			if (Time.timeScale == 0f)
			{
				return true;
			}
			if (!Probe("Chat", "HasFocus", isStatic: false) && !Probe("Console", "IsVisible", isStatic: true) && !Probe("InventoryGui", "IsVisible", isStatic: true) && !Probe("Menu", "IsVisible", isStatic: true) && !Probe("TextInput", "IsVisible", isStatic: true))
			{
				return Probe("StoreGui", "IsVisible", isStatic: true);
			}
			return true;
		}

		private static bool Probe(string typeName, string methodName, bool isStatic)
		{
			try
			{
				Type type = typeof(Player).Assembly.GetType(typeName);
				if (type == null)
				{
					return false;
				}
				MethodInfo method = type.GetMethod(methodName, (BindingFlags)(0x30 | (isStatic ? 8 : 4)), null, Type.EmptyTypes, null);
				if (method == null)
				{
					return true;
				}
				object obj = null;
				if (!isStatic)
				{
					obj = type.GetProperty("instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(null, null) ?? type.GetField("instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(null);
					if (obj == null)
					{
						return false;
					}
				}
				return (bool)method.Invoke(obj, null);
			}
			catch
			{
				return true;
			}
		}
	}
	internal sealed class MapPin
	{
		private readonly Plugin plugin;

		private Minimap map;

		private object pin;

		private bool failed;

		internal MapPin(Plugin plugin)
		{
			this.plugin = plugin;
		}

		internal void Update(Vector3 position, string name)
		{
			//IL_0158: 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 ((Object)(object)Minimap.instance == (Object)null)
			{
				return;
			}
			if ((Object)(object)map != (Object)(object)Minimap.instance)
			{
				Remove();
				map = Minimap.instance;
				failed = false;
			}
			if (failed)
			{
				return;
			}
			try
			{
				if (pin == null)
				{
					MethodInfo methodInfo = typeof(Minimap).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).First((MethodInfo m) => m.Name == "AddPin" && m.GetParameters().Length >= 5 && m.GetParameters()[0].ParameterType == typeof(Vector3));
					ParameterInfo[] parameters = methodInfo.GetParameters();
					object[] array = new object[parameters.Length];
					array[0] = position;
					array[1] = Enum.Parse(parameters[1].ParameterType, "Icon3");
					array[2] = name;
					array[3] = false;
					array[4] = false;
					for (int num = 5; num < array.Length; num++)
					{
						array[num] = ((parameters[num].ParameterType == typeof(string)) ? "" : (parameters[num].ParameterType.IsValueType ? Activator.CreateInstance(parameters[num].ParameterType) : null));
					}
					pin = methodInfo.Invoke(map, array);
					if (pin == null)
					{
						throw new InvalidOperationException("AddPin returned null");
					}
				}
				pin.GetType().GetField("m_pos").SetValue(pin, position);
				pin.GetType().GetField("m_name").SetValue(pin, name);
			}
			catch (Exception ex)
			{
				failed = true;
				plugin.LogWarning("Ludvig map pin unavailable in this build: " + ex);
			}
		}

		internal void Remove()
		{
			if (pin != null && (Object)(object)map != (Object)null)
			{
				try
				{
					typeof(Minimap).GetMethod("RemovePin", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[1] { pin.GetType() }, null)?.Invoke(map, new object[1] { pin });
				}
				catch (Exception ex)
				{
					plugin.LogWarning("Ludvig map pin cleanup: " + ex.Message);
				}
			}
			pin = null;
			map = null;
		}
	}
	internal static class Geometry
	{
		private static string ReadString(BinaryReader r)
		{
			int num = r.ReadInt32();
			if (num < 0 || num > 1048576)
			{
				throw new InvalidDataException("Invalid geometry string length: " + num);
			}
			byte[] array = r.ReadBytes(num);
			if (array.Length != num)
			{
				throw new EndOfStreamException();
			}
			return Encoding.UTF8.GetString(array);
		}

		private static float[] ReadFloats(BinaryReader r)
		{
			int num = r.ReadInt32();
			if (num < 0 || num > 50000000)
			{
				throw new InvalidDataException("Invalid float array length: " + num);
			}
			float[] array = new float[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = r.ReadSingle();
			}
			return array;
		}

		private static int[] ReadInts(BinaryReader r)
		{
			int num = r.ReadInt32();
			if (num < 0 || num > 50000000)
			{
				throw new InvalidDataException("Invalid int array length: " + num);
			}
			int[] array = new int[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = r.ReadInt32();
			}
			return array;
		}

		internal static Dictionary<string, Mesh> Load(List<Mesh> owned, Plugin owner)
		{
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0274: Expected O, but got Unknown
			using Stream stream = typeof(Geometry).Assembly.GetManifestResourceStream("LudvigValheim.mesh_shapes.bin");
			if (stream == null)
			{
				throw new InvalidDataException("Missing embedded resource: LudvigValheim.mesh_shapes.bin");
			}
			owner.LogInfo("mesh_shapes.bin bytes: " + stream.Length);
			using BinaryReader binaryReader = new BinaryReader(stream, Encoding.UTF8);
			string text = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
			if (text != "LVG1")
			{
				throw new InvalidDataException("Bad geometry header: " + text);
			}
			int num = binaryReader.ReadInt32();
			if (num <= 0 || num > 1000)
			{
				throw new InvalidDataException("Invalid shape count: " + num);
			}
			Dictionary<string, Mesh> dictionary = new Dictionary<string, Mesh>();
			for (int i = 0; i < num; i++)
			{
				string text2 = ReadString(binaryReader);
				float[] array = ReadFloats(binaryReader);
				float[] array2 = ReadFloats(binaryReader);
				float[] array3 = ReadFloats(binaryReader);
				float[] array4 = ReadFloats(binaryReader);
				int[] array5 = ReadInts(binaryReader);
				if (string.IsNullOrEmpty(text2) || array.Length == 0 || array.Length % 3 != 0)
				{
					throw new InvalidDataException("Invalid geometry shape header: " + text2);
				}
				int num2 = array.Length / 3;
				if (array2.Length != num2 * 3 || array3.Length != num2 * 4 || array4.Length != num2 * 2 || array5.Length == 0 || array5.Length % 3 != 0)
				{
					throw new InvalidDataException("Invalid vertex attributes: " + text2);
				}
				Vector3[] array6 = (Vector3[])(object)new Vector3[num2];
				Vector3[] array7 = (Vector3[])(object)new Vector3[num2];
				Vector2[] array8 = (Vector2[])(object)new Vector2[num2];
				Vector4[] array9 = (Vector4[])(object)new Vector4[num2];
				for (int j = 0; j < num2; j++)
				{
					array6[j] = new Vector3(array[j * 3], array[j * 3 + 1], array[j * 3 + 2]);
					array7[j] = new Vector3(array2[j * 3], array2[j * 3 + 1], array2[j * 3 + 2]);
					array8[j] = new Vector2(array4[j * 2], array4[j * 2 + 1]);
					array9[j] = new Vector4(array3[j * 4], array3[j * 4 + 1], array3[j * 4 + 2], array3[j * 4 + 3]);
				}
				int[] array10 = array5;
				foreach (int num3 in array10)
				{
					if (num3 < 0 || num3 >= num2)
					{
						throw new InvalidDataException("Invalid triangle index: " + text2);
					}
				}
				Mesh val = new Mesh();
				owned.Add(val);
				((Object)val).name = "Ludvig geometry " + text2;
				val.indexFormat = (IndexFormat)1;
				val.vertices = array6;
				val.normals = array7;
				val.uv = array8;
				val.tangents = array9;
				val.triangles = array5;
				val.RecalculateBounds();
				dictionary.Add(text2, val);
			}
			owner.LogInfo("Ludvig binary geometry decoded: " + dictionary.Count + " shapes");
			return dictionary;
		}
	}
	internal sealed class LudvigAudio : MonoBehaviour
	{
		private Plugin plugin;

		private AudioSource source;

		private readonly Dictionary<string, List<AudioClip>> groups = new Dictionary<string, List<AudioClip>>(StringComparer.OrdinalIgnoreCase);

		private readonly List<AudioClip> owned = new List<AudioClip>();

		private float enemyCooldown;

		internal bool IsSpeaking
		{
			get
			{
				if ((Object)(object)source != (Object)null)
				{
					return source.isPlaying;
				}
				return false;
			}
		}

		internal void Init(Plugin owner)
		{
			plugin = owner;
			source = ((Component)this).gameObject.AddComponent<AudioSource>();
			source.spatialBlend = 1f;
			source.minDistance = plugin.VoiceDistance.Value;
			source.maxDistance = Mathf.Max(32f, source.minDistance + 8f);
			source.rolloffMode = (AudioRolloffMode)1;
			source.playOnAwake = false;
			source.volume = 1f;
			string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Sounds");
			if (!Directory.Exists(text))
			{
				plugin.LogWarning("Sounds folder missing: " + text);
				return;
			}
			string[] files = Directory.GetFiles(text, "ludvig_*.wav");
			foreach (string text2 in files)
			{
				try
				{
					AudioClip item = LoadWav(text2, plugin.VoiceGain.Value);
					owned.Add(item);
					string text3 = Path.GetFileNameWithoutExtension(text2).ToLowerInvariant();
					int length = "ludvig_".Length;
					int num = text3.LastIndexOf('_');
					string key = ((num > length) ? text3.Substring(length, num - length) : text3.Substring(length));
					if (!groups.TryGetValue(key, out var value))
					{
						value = (groups[key] = new List<AudioClip>());
					}
					value.Add(item);
				}
				catch (Exception ex)
				{
					plugin.LogWarning("Could not load sound " + text2 + ": " + ex.Message);
				}
			}
			foreach (List<AudioClip> value2 in groups.Values)
			{
				if (value2.Exists((AudioClip c) => IsNumberedRecording(((Object)c).name)))
				{
					value2.RemoveAll((AudioClip c) => !IsNumberedRecording(((Object)c).name));
				}
			}
			plugin.LogInfo("Ludvig audio loaded: " + owned.Count + " clips from " + text);
		}

		private static bool IsNumberedRecording(string name)
		{
			string text = name.Substring(name.LastIndexOf('_') + 1);
			if (text.Length == 3 && int.TryParse(text, out var result) && result >= 1)
			{
				return result <= 165;
			}
			return false;
		}

		internal void Play(string group)
		{
			if ((Object)(object)source == (Object)null)
			{
				return;
			}
			bool flag = group == "low_health" || group == "player_death";
			if (source.isPlaying && !flag)
			{
				return;
			}
			if (group.StartsWith("ore_") && !groups.ContainsKey(group) && groups.ContainsKey("ore"))
			{
				group = "ore";
			}
			if (!groups.ContainsKey(group) && (group == "home_walk" || group == "home_missing"))
			{
				group = "home";
			}
			if (!groups.ContainsKey(group))
			{
				switch (group)
				{
				case "trophy_start":
				case "hunt_wolf":
				case "hunt_lox":
				case "hunt_hare":
					group = "hunt_start";
					break;
				}
			}
			if (group == "home" && groups.ContainsKey("home_arrive"))
			{
				group = "home_arrive";
			}
			if (groups.TryGetValue(group, out var value) && value.Count != 0)
			{
				source.clip = value[Random.Range(0, value.Count)];
				source.Play();
			}
		}

		internal void TickEnemy(float dt)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			enemyCooldown -= dt;
			if (enemyCooldown > 0f || (Object)(object)source == (Object)null || source.isPlaying)
			{
				return;
			}
			Collider[] array = Physics.OverlapSphere(((Component)this).transform.position, 9f);
			foreach (Collider val in array)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				string text = ((Object)((Component)val).transform.root).name.ToLowerInvariant();
				string text2 = (text.Contains("eikthyr") ? "boss_eikthyr" : (text.Contains("gdking") ? "boss_elder" : (text.Contains("bonemass") ? "boss_bonemass" : (text.Contains("dragon") ? "boss_moder" : (text.Contains("goblinking") ? "boss_yagluth" : (text.Contains("seekerqueen") ? "boss_queen" : (text.Contains("fader") ? "boss_fader" : (text.Contains("deathsquito") ? "enemy_deathsquito" : (text.Contains("draugr") ? "enemy_draugr" : null)))))))));
				if (text2 != null)
				{
					enemyCooldown = 24f;
					Play(text2);
					break;
				}
				if (text.Contains("greyling") || text.Contains("greydwarf") || text.Contains("troll") || text.Contains("skeleton"))
				{
					enemyCooldown = 18f;
					if (text.Contains("greydwarf"))
					{
						Play("enemy_greydwarf");
					}
					else if (text.Contains("greyling"))
					{
						Play("enemy_greyling");
					}
					else if (text.Contains("skeleton"))
					{
						Play("enemy_skeleton");
					}
					else if (text.Contains("troll"))
					{
						Play("enemy_troll");
					}
					if (!IsSpeaking)
					{
						Play("enemy");
					}
					break;
				}
			}
		}

		internal static AudioClip LoadWav(string path, float gain = 1f)
		{
			byte[] array = File.ReadAllBytes(path);
			if (array.Length