Decompiled source of The Lyngvi Wardens v1.3.0

TheLyngviWardens.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("The-Lyngvi-Wardens")]
[assembly: AssemblyDescription("Valheim Companion Mod")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("The-Lyngvi-Wardens")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a1b2c3d4-e5f6-7890-abcd-ef1234567890")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SoulboundCompanion;

internal enum WardenRole
{
	Tank,
	Berserker,
	Archer,
	Skirmisher
}
[BepInPlugin("com.forlore.soulboundcompanion", "The Lyngvi Wardens", "1.3.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
internal class SoulboundCompanionPlugin : BaseUnityPlugin
{
	private sealed class WardenState
	{
		public int Slot;

		public WardenRole Role;

		public GameObject Object;

		public MonsterAI AI;

		public bool Spawned;

		public float SpawnDelay;

		public bool RespawnPending;

		public float RespawnTimer;

		public long RespawnOwnerId;

		public bool ReportedPersistentSearch;

		public float MovementCheckTimer;

		public float StuckTime;

		public Vector3 LastPosition;

		public float PatrolTimer;

		public Vector3 PatrolAnchor;

		public bool HasPatrolPoint;

		public bool WasInCombat;

		public bool SoftLeashIssued;

		public float PersonalSpaceCooldown;

		public float BlockHoldTimer;

		public float CombatMoveTimer;

		public Vector3 CombatMovePoint;

		public bool HasCombatMovePoint;

		public WardenState(int slot, WardenRole role)
		{
			Slot = slot;
			Role = role;
		}
	}

	private sealed class ProgressionPower
	{
		public int Stage;

		public int Level;

		public float ElementalDamage;

		public string Name;

		public ProgressionPower(int stage, int level, float elementalDamage, string name)
		{
			Stage = stage;
			Level = level;
			ElementalDamage = elementalDamage;
			Name = name;
		}
	}

	[HarmonyPatch(typeof(Character), "OnDeath")]
	private static class WardenDeathPatch
	{
		private static void Prefix(Character __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Object)(object)Instance != (Object)null && (Instance._isDismissOperation || Instance._wardensDismissed)))
			{
				return;
			}
			LyngviWardenMarker component = ((Component)__instance).GetComponent<LyngviWardenMarker>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			ZNetView component2 = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component2 == (Object)null || !component2.IsValid())
			{
				return;
			}
			ZDO zDO = component2.GetZDO();
			if (zDO == null)
			{
				return;
			}
			long num = zDO.GetLong("SoulboundCompanion_OwnerId", 0L);
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null) && num == localPlayer.GetPlayerID())
			{
				int slot = zDO.GetInt("SoulboundCompanion_Slot", component.Slot);
				zDO.Set("SoulboundCompanion_IsSoulbound", false);
				if ((Object)(object)Instance != (Object)null)
				{
					Instance.ScheduleRespawn(slot, num);
				}
			}
		}
	}

	[HarmonyPatch(typeof(Character), "Damage", new Type[] { typeof(HitData) })]
	private static class WardenElementalDamagePatch
	{
		private static void Prefix(Character __instance, HitData hit)
		{
			if (hit == null || !hit.HaveAttacker())
			{
				return;
			}
			Character attacker = hit.GetAttacker();
			if ((Object)(object)attacker == (Object)null)
			{
				return;
			}
			if ((Object)(object)Instance != (Object)null)
			{
				Instance.ObserveCombatHit(__instance, attacker);
			}
			LyngviWardenMarker component = ((Component)attacker).GetComponent<LyngviWardenMarker>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			float elementalDamage = component.ElementalDamage;
			if (!(elementalDamage <= 0f))
			{
				switch (component.Slot)
				{
				case 0:
					hit.m_damage.m_poison += elementalDamage;
					break;
				case 1:
					hit.m_damage.m_fire += elementalDamage;
					break;
				case 2:
					hit.m_damage.m_frost += elementalDamage;
					break;
				case 3:
					hit.m_damage.m_lightning += elementalDamage;
					break;
				}
			}
		}
	}

	[HarmonyPatch(typeof(MonsterAI), "UpdateAI")]
	private static class WardenRoleAIPatch
	{
		private static void Postfix(MonsterAI __instance, float dt)
		{
			if (!((Object)(object)Instance == (Object)null))
			{
				Instance.UpdateSpecialCombatBehaviour(__instance, dt);
			}
		}
	}

	[HarmonyPatch(typeof(MonsterAI), "Awake")]
	private static class MonsterAIAwakePatch
	{
		private static void Postfix(MonsterAI __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null || !zDO.GetBool("SoulboundCompanion_IsSoulbound", false))
			{
				return;
			}
			int num = zDO.GetInt("SoulboundCompanion_Slot", -1);
			if (num >= 0 && num <= 3)
			{
				WardenRole role = (WardenRole)zDO.GetInt("SoulboundCompanion_Role", num);
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer != (Object)null && zDO.GetLong("SoulboundCompanion_OwnerId", 0L) == localPlayer.GetPlayerID())
				{
					EnsureWardenState(((Component)__instance).gameObject, num, role, localPlayer);
				}
				else
				{
					EnsureWardenState(((Component)__instance).gameObject, num, role, null);
				}
			}
		}
	}

	public const string PluginGUID = "com.forlore.soulboundcompanion";

	public const string PluginName = "The Lyngvi Wardens";

	public const string PluginVersion = "1.3.0";

	public const string BodvarName = "Bodvar";

	public const string StarkadName = "Starkad";

	public const string OrvarName = "Orvar";

	public const string EgilName = "Egil";

	private const float BodvarBaseHealth = 150f;

	private const float StarkadBaseHealth = 45f;

	private const float OrvarBaseHealth = 40f;

	private const float EgilBaseHealth = 45f;

	public const float RespawnDelaySeconds = 20f;

	public const float RegenerationPercentPerSecond = 0.02f;

	public const float RegenerationTickInterval = 1f;

	private const float CombatSenseInterval = 0.15f;

	private const float SharedThreatRange = 45f;

	private const float SharedThreatMemorySeconds = 6f;

	private const float BodvarBlockSenseRange = 7f;

	private const float BodvarBlockHoldSeconds = 0.65f;

	private const float OrvarPreferredCombatDistance = 16f;

	private const float OrvarRetreatTriggerDistance = 13f;

	private const float OrvarEmergencyDistance = 8f;

	private const float EgilPreferredCombatDistance = 10.5f;

	private const float EgilRetreatTriggerDistance = 6.5f;

	private const float EgilBacklineProtectionRadius = 13f;

	private const float EgilIndependentSearchRadius = 32f;

	private const float EgilAiAttackInterval = 1.35f;

	private const float ProgressionCheckInterval = 2f;

	private float _progressionCheckTimer;

	private int _activeProgressionStage = -1;

	private const float BodvarPatrolMinDistance = 12f;

	private const float BodvarPatrolMaxDistance = 18f;

	private const float BodvarPersonalSpaceDistance = 8.5f;

	private const float BodvarPersonalSpaceRefreshSeconds = 2.5f;

	private const float StarkadPatrolMinDistance = 8f;

	private const float StarkadPatrolMaxDistance = 14f;

	private const float OrvarPatrolMinDistance = 12f;

	private const float OrvarPatrolMaxDistance = 18f;

	private const float EgilPatrolMinDistance = 10f;

	private const float EgilPatrolMaxDistance = 16f;

	private const float InteriorMeleePatrolMinDistance = 3f;

	private const float InteriorMeleePatrolMaxDistance = 5f;

	private const float InteriorOrvarPatrolMinDistance = 4.5f;

	private const float InteriorOrvarPatrolMaxDistance = 6.5f;

	private const float InteriorEgilPatrolMinDistance = 4f;

	private const float InteriorEgilPatrolMaxDistance = 6f;

	private const float PatrolTimeMin = 8f;

	private const float PatrolTimeMax = 14f;

	private const float PatrolRecenterDistance = 12f;

	private const float SoftLeashDistance = 30f;

	private const float SoftLeashReleaseDistance = 24f;

	private const float HardTeleportDistance = 70f;

	private const float StuckCheckDistance = 32f;

	private const float MovementCheckInterval = 1f;

	private const float MinimumMovement = 0.35f;

	private const float StuckTimeBeforeTeleport = 8f;

	private const float InteriorRescueDistance = 7.5f;

	private const float InteriorMinimumMovement = 0.2f;

	private const float InteriorStuckTimeBeforeTeleport = 3f;

	private const float FriendlyCollisionRefreshInterval = 1f;

	private const float InteriorCheckInterval = 0.25f;

	private const float InteriorTransferDelay = 0.6f;

	private const float PostInteriorTransferGrace = 2f;

	private float _interiorCheckTimer;

	private bool _hasInteriorState;

	private bool _playerWasInInterior;

	private bool _interiorTransferPending;

	private bool _pendingInteriorState;

	private float _interiorTransferTimer;

	private float _postInteriorTransferGraceTimer;

	private const string BodvarPrefabName = "LyngviWarden_Bodvar_110";

	private const string PreviousBodvarPrefabName = "LyngviWarden_Bodvar";

	private const string BodvarShieldPrefabName = "LyngviWarden_BodvarShield";

	private const string StarkadPrefabName = "LyngviWarden_Starkad";

	private const string OrvarPrefabName = "LyngviWarden_Orvar_120";

	private const string PreviousOrvarPrefabName = "LyngviWarden_Orvar_110";

	private const string OlderOrvarPrefabName = "LyngviWarden_Orvar";

	private const string EgilPrefabName = "LyngviWarden_Egil_120";

	private const string PreviousEgilPrefabName = "LyngviWarden_Egil_Charred";

	private const string OlderEgilPrefabName = "LyngviWarden_Egil";

	private const string OrvarBowPrefabName = "LyngviWarden_OrvarBow_120";

	private const string EgilBowPrefabName = "LyngviWarden_EgilBow_120";

	private const string LegacyWarriorPrefabName = "SoulboundCompanion_Warrior";

	private const string LegacyArcherPrefabName = "SoulboundCompanion_Archer";

	private const string AncientLegacyPrefabName = "Skeleton_NoArcher";

	private bool _legacyMigrationDone;

	private bool _prefabsReady;

	internal const string ZdoSoulbound = "SoulboundCompanion_IsSoulbound";

	internal const string ZdoOwnerId = "SoulboundCompanion_OwnerId";

	internal const string ZdoOwnerName = "SoulboundCompanion_OwnerName";

	internal const string ZdoSlot = "SoulboundCompanion_Slot";

	internal const string ZdoRole = "SoulboundCompanion_Role";

	internal const string ZdoScaledLevel = "SoulboundCompanion_ScaledLevel";

	internal const string ZdoElementalDamage = "SoulboundCompanion_ElementalDamage";

	private const string PlayerDataDismissedKey = "TheLyngviWardens.Dismissed";

	private const string PlayerDataHealthPrefix = "TheLyngviWardens.Health.";

	private const string PlayerDataRespawnPrefix = "TheLyngviWardens.Respawn.";

	internal static SoulboundCompanionPlugin Instance;

	private Harmony _harmony;

	private Player _currentPlayer;

	private ConfigEntry<KeyboardShortcut> _summonDismissShortcut;

	private ButtonConfig _summonDismissButton;

	private bool _wardensDismissed;

	private bool _dismissCleanupNeeded;

	private bool _isDismissOperation;

	private float _friendlyCollisionRefreshTimer;

	private Character _sharedThreat;

	private float _sharedThreatTimer;

	private float _combatSenseTimer;

	private static readonly MethodInfo BaseAIMoveToMethod = AccessTools.Method(typeof(BaseAI), "MoveTo", new Type[4]
	{
		typeof(float),
		typeof(Vector3),
		typeof(float),
		typeof(bool)
	}, (Type[])null);

	private static readonly MethodInfo BaseAILookAtMethod = AccessTools.Method(typeof(BaseAI), "LookAt", new Type[1] { typeof(Vector3) }, (Type[])null);

	private static readonly FieldInfo CharacterBlockingField = AccessTools.Field(typeof(Character), "m_blocking");

	private static readonly FieldInfo HumanoidBlockTimerField = AccessTools.Field(typeof(Humanoid), "m_blockTimer");

	private static readonly MethodInfo HumanoidGetCurrentBlockerMethod = AccessTools.Method(typeof(Humanoid), "GetCurrentBlocker", (Type[])null, (Type[])null);

	private WardenState[] _wardens;

	private void Awake()
	{
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Expected O, but got Unknown
		Instance = this;
		CreateControlBindings();
		CommandManager.Instance.AddConsoleCommand((ConsoleCommand)(object)new WardensConsoleCommand());
		_wardens = new WardenState[4]
		{
			new WardenState(0, WardenRole.Tank),
			new WardenState(1, WardenRole.Berserker),
			new WardenState(2, WardenRole.Archer),
			new WardenState(3, WardenRole.Skirmisher)
		};
		_harmony = new Harmony("com.forlore.soulboundcompanion");
		_harmony.PatchAll();
		PrefabManager.OnVanillaPrefabsAvailable += CreateWardenPrefabs;
		Logger.LogInfo((object)"The Lyngvi Wardens have awakened!");
	}

	private void CreateControlBindings()
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Expected O, but got Unknown
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Expected O, but got Unknown
		((BaseUnityPlugin)this).Config.SaveOnConfigSet = true;
		_summonDismissShortcut = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Controls", "SummonDismissWardens", new KeyboardShortcut((KeyCode)122, (KeyCode[])(object)new KeyCode[1] { (KeyCode)308 }), new ConfigDescription("Summon or dismiss all Lyngvi Wardens. Default: Left Alt + Z.", (AcceptableValueBase)null, Array.Empty<object>()));
		_summonDismissButton = new ButtonConfig
		{
			Name = "LyngviWardens_SummonDismiss",
			ShortcutConfig = _summonDismissShortcut,
			Hint = "Summon / dismiss Wardens",
			ActiveInGUI = false,
			ActiveInCustomGUI = false,
			BlockOtherInputs = false
		};
		InputManager.Instance.AddButton("com.forlore.soulboundcompanion", _summonDismissButton);
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
		PrefabManager.OnVanillaPrefabsAvailable -= CreateWardenPrefabs;
		if ((Object)(object)Instance == (Object)(object)this)
		{
			Instance = null;
		}
	}

	private void CreateWardenPrefabs()
	{
		CreateBodvarShieldPrefab();
		CreateOrvarBowPrefab();
		CreateEgilBowPrefab();
		CreateBodvarPrefab();
		CreateStarkadPrefab();
		CreateOrvarPrefab();
		CreateEgilPrefab();
		_prefabsReady = (Object)(object)PrefabManager.Instance.GetPrefab("LyngviWarden_BodvarShield") != (Object)null && (Object)(object)PrefabManager.Instance.GetPrefab("LyngviWarden_OrvarBow_120") != (Object)null && (Object)(object)PrefabManager.Instance.GetPrefab("LyngviWarden_EgilBow_120") != (Object)null && (Object)(object)PrefabManager.Instance.GetPrefab("LyngviWarden_Bodvar_110") != (Object)null && (Object)(object)PrefabManager.Instance.GetPrefab("LyngviWarden_Starkad") != (Object)null && (Object)(object)PrefabManager.Instance.GetPrefab("LyngviWarden_Orvar_120") != (Object)null && (Object)(object)PrefabManager.Instance.GetPrefab("LyngviWarden_Egil_120") != (Object)null;
		if (_prefabsReady)
		{
			Logger.LogInfo((object)"Bodvar, Starkad, Orvar and Egil prefabs are ready.");
			PrefabManager.OnVanillaPrefabsAvailable -= CreateWardenPrefabs;
		}
	}

	private void CreateBodvarShieldPrefab()
	{
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		if ((Object)(object)PrefabManager.Instance.GetPrefab("LyngviWarden_BodvarShield") != (Object)null)
		{
			return;
		}
		try
		{
			CustomItem val = new CustomItem("LyngviWarden_BodvarShield", "ShieldGoldTower");
			if ((Object)(object)val.ItemDrop == (Object)null || val.ItemDrop.m_itemData == null || val.ItemDrop.m_itemData.m_shared == null)
			{
				Logger.LogError((object)"Failed to clone Bodvar's Nord Greatshield.");
				return;
			}
			val.ItemDrop.m_itemData.m_shared.m_blockPower = GetBodvarShieldBlockPowerForStage(0);
			val.ItemDrop.m_itemData.m_shared.m_blockPowerPerLevel = 0f;
			val.ItemDrop.m_itemData.m_shared.m_timedBlockBonus = 1f;
			ItemManager.Instance.AddItem(val);
			Logger.LogInfo((object)"Created Bodvar's progression-scaled Nord Greatshield.");
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Could not create Bodvar's Greatshield: {arg}");
		}
	}

	private void CreateOrvarBowPrefab()
	{
		CreateNpcBowWithVisual("LyngviWarden_OrvarBow_120", "charred_bow", "Orvar's Longbow", -1f);
	}

	private void CreateEgilBowPrefab()
	{
		CreateNpcBowWithVisual("LyngviWarden_EgilBow_120", "BowFineWood", "Egil's Finewood Bow", 1.35f);
	}

	private void CreateNpcBowWithVisual(string customPrefabName, string visualSourcePrefabName, string logName, float aiAttackIntervalOverride)
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Expected O, but got Unknown
		if ((Object)(object)PrefabManager.Instance.GetPrefab(customPrefabName) != (Object)null)
		{
			return;
		}
		try
		{
			CustomItem val = new CustomItem(customPrefabName, "skeleton_bow");
			if ((Object)(object)val.ItemPrefab == (Object)null || (Object)(object)val.ItemDrop == (Object)null)
			{
				Logger.LogError((object)("Failed to clone NPC bow for " + logName + "."));
				return;
			}
			GameObject prefab = PrefabManager.Instance.GetPrefab(visualSourcePrefabName);
			if ((Object)(object)prefab == (Object)null)
			{
				Logger.LogError((object)("Could not find visual source '" + visualSourcePrefabName + "' for " + logName + "."));
				return;
			}
			if (!ReplaceEquipmentAttachVisual(val.ItemPrefab, prefab))
			{
				Logger.LogWarning((object)(logName + ": could not swap the attach visual. The bow will still function, but may retain the skeleton-bow appearance."));
			}
			if (aiAttackIntervalOverride > 0f && val.ItemDrop.m_itemData != null && val.ItemDrop.m_itemData.m_shared != null)
			{
				val.ItemDrop.m_itemData.m_shared.m_aiAttackInterval = aiAttackIntervalOverride;
			}
			ItemManager.Instance.AddItem(val);
			Logger.LogInfo((object)("Created " + logName + ": skeleton NPC bow mechanics with '" + visualSourcePrefabName + "' appearance."));
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Could not create {logName}: {arg}");
		}
	}

	private static bool ReplaceEquipmentAttachVisual(GameObject targetItem, GameObject visualSource)
	{
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)targetItem == (Object)null || (Object)(object)visualSource == (Object)null)
		{
			return false;
		}
		Transform val = FindChildRecursive(targetItem.transform, "attach");
		Transform val2 = FindChildRecursive(visualSource.transform, "attach");
		if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null || (Object)(object)val.parent == (Object)null)
		{
			return false;
		}
		Transform parent = val.parent;
		int siblingIndex = val.GetSiblingIndex();
		GameObject val3 = Object.Instantiate<GameObject>(((Component)val2).gameObject, parent, false);
		((Object)val3).name = "attach";
		val3.transform.localPosition = val2.localPosition;
		val3.transform.localRotation = val2.localRotation;
		val3.transform.localScale = val2.localScale;
		val3.transform.SetSiblingIndex(siblingIndex);
		Object.DestroyImmediate((Object)(object)((Component)val).gameObject);
		return true;
	}

	private static Transform FindChildRecursive(Transform root, string childName)
	{
		if ((Object)(object)root == (Object)null)
		{
			return null;
		}
		if (string.Equals(((Object)root).name, childName, StringComparison.OrdinalIgnoreCase))
		{
			return root;
		}
		for (int i = 0; i < root.childCount; i++)
		{
			Transform val = FindChildRecursive(root.GetChild(i), childName);
			if ((Object)(object)val != (Object)null)
			{
				return val;
			}
		}
		return null;
	}

	private void CreateBodvarPrefab()
	{
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_015f: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: 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_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		GameObject prefab = PrefabManager.Instance.GetPrefab("LyngviWarden_Bodvar_110");
		if ((Object)(object)prefab != (Object)null)
		{
			return;
		}
		prefab = PrefabManager.Instance.CreateClonedPrefab("LyngviWarden_Bodvar_110", "Skeleton_Poison");
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogError((object)"Failed to create Bodvar from Skeleton_Poison.");
			return;
		}
		Humanoid component = prefab.GetComponent<Humanoid>();
		Character component2 = prefab.GetComponent<Character>();
		MonsterAI component3 = prefab.GetComponent<MonsterAI>();
		if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null || (Object)(object)component3 == (Object)null)
		{
			Logger.LogError((object)"Bodvar prefab is missing Humanoid/Character/MonsterAI.");
			return;
		}
		GameObject prefab2 = PrefabManager.Instance.GetPrefab("Skeleton");
		MonsterAI val = (((Object)(object)prefab2 != (Object)null) ? prefab2.GetComponent<MonsterAI>() : null);
		if ((Object)(object)val != (Object)null)
		{
			((BaseAI)component3).m_pathAgentType = ((BaseAI)val).m_pathAgentType;
		}
		component.m_defaultItems = (GameObject[])(object)new GameObject[0];
		component.m_randomWeapon = ResolvePrefabs("skeleton_mace");
		component.m_randomArmor = (GameObject[])(object)new GameObject[0];
		component.m_randomShield = ResolvePrefabs("LyngviWarden_BodvarShield");
		component2.m_name = "Bodvar";
		component2.m_health = 150f;
		component2.m_boss = false;
		component2.m_damageModifiers.m_blunt = (DamageModifier)0;
		component2.m_damageModifiers.m_slash = (DamageModifier)0;
		component2.m_damageModifiers.m_pierce = (DamageModifier)1;
		component2.m_damageModifiers.m_fire = (DamageModifier)0;
		component2.m_damageModifiers.m_frost = (DamageModifier)1;
		component2.m_damageModifiers.m_poison = (DamageModifier)3;
		component2.m_damageModifiers.m_spirit = (DamageModifier)3;
		DisableDrops(prefab);
		PrefabManager.Instance.AddPrefab(prefab);
		Logger.LogInfo((object)"Created Bodvar: 150 base HP, tank resistances, mace + Nord Greatshield.");
	}

	private void CreateStarkadPrefab()
	{
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		GameObject prefab = PrefabManager.Instance.GetPrefab("LyngviWarden_Starkad");
		if ((Object)(object)prefab != (Object)null)
		{
			return;
		}
		prefab = PrefabManager.Instance.CreateClonedPrefab("LyngviWarden_Starkad", "Skeleton_Hildir");
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogError((object)"Failed to create Starkad from Skeleton_Hildir.");
			return;
		}
		Humanoid component = prefab.GetComponent<Humanoid>();
		Character component2 = prefab.GetComponent<Character>();
		MonsterAI component3 = prefab.GetComponent<MonsterAI>();
		if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null || (Object)(object)component3 == (Object)null)
		{
			Logger.LogError((object)"Starkad prefab is missing Humanoid/Character/MonsterAI.");
			return;
		}
		GameObject prefab2 = PrefabManager.Instance.GetPrefab("Skeleton");
		MonsterAI val = (((Object)(object)prefab2 != (Object)null) ? prefab2.GetComponent<MonsterAI>() : null);
		if ((Object)(object)val != (Object)null)
		{
			((BaseAI)component3).m_pathAgentType = ((BaseAI)val).m_pathAgentType;
		}
		component.m_defaultItems = (GameObject[])(object)new GameObject[0];
		component.m_randomWeapon = ResolvePrefabs("skeleton_sword_hildir");
		component.m_randomArmor = (GameObject[])(object)new GameObject[0];
		component.m_randomShield = (GameObject[])(object)new GameObject[0];
		component2.m_name = "Starkad";
		component2.m_health = 45f;
		component2.m_boss = false;
		DisableDrops(prefab);
		PrefabManager.Instance.AddPrefab(prefab);
		Logger.LogInfo((object)"Created Starkad: burning Brenna body, fixed flaming sword, no fire nova.");
	}

	private void CreateOrvarPrefab()
	{
		GameObject prefab = PrefabManager.Instance.GetPrefab("LyngviWarden_Orvar_120");
		if ((Object)(object)prefab != (Object)null)
		{
			return;
		}
		prefab = PrefabManager.Instance.CreateClonedPrefab("LyngviWarden_Orvar_120", "Skeleton");
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogError((object)"Failed to create Orvar from Skeleton.");
			return;
		}
		Humanoid component = prefab.GetComponent<Humanoid>();
		Character component2 = prefab.GetComponent<Character>();
		MonsterAI component3 = prefab.GetComponent<MonsterAI>();
		if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null || (Object)(object)component3 == (Object)null)
		{
			Logger.LogError((object)"Orvar prefab is missing Humanoid/Character/MonsterAI.");
			return;
		}
		component.m_defaultItems = (GameObject[])(object)new GameObject[0];
		component.m_randomWeapon = ResolvePrefabs("LyngviWarden_OrvarBow_120");
		component.m_randomArmor = (GameObject[])(object)new GameObject[0];
		component.m_randomShield = (GameObject[])(object)new GameObject[0];
		component2.m_name = "Orvar";
		component2.m_health = 40f;
		component2.m_boss = false;
		component3.m_circleTargetDistance = 16f;
		component3.m_circleTargetInterval = 4f;
		component3.m_circleTargetDuration = 0.75f;
		DisableDrops(prefab);
		PrefabManager.Instance.AddPrefab(prefab);
		Logger.LogInfo((object)"Created Orvar: frost sniper with fixed Charred longbow appearance and long-range combat profile.");
	}

	private void CreateEgilPrefab()
	{
		//IL_0101: 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_0119: 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_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_013d: 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_0155: Unknown result type (might be due to invalid IL or missing references)
		GameObject prefab = PrefabManager.Instance.GetPrefab("LyngviWarden_Egil_120");
		if ((Object)(object)prefab != (Object)null)
		{
			return;
		}
		prefab = PrefabManager.Instance.CreateClonedPrefab("LyngviWarden_Egil_120", "Skeleton");
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogError((object)"Failed to create Egil from Skeleton.");
			return;
		}
		Humanoid component = prefab.GetComponent<Humanoid>();
		Character component2 = prefab.GetComponent<Character>();
		MonsterAI component3 = prefab.GetComponent<MonsterAI>();
		if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null || (Object)(object)component3 == (Object)null)
		{
			Logger.LogError((object)"Egil prefab is missing Humanoid/Character/MonsterAI.");
			return;
		}
		component.m_defaultItems = (GameObject[])(object)new GameObject[0];
		component.m_randomWeapon = ResolvePrefabs("LyngviWarden_EgilBow_120");
		component.m_randomArmor = (GameObject[])(object)new GameObject[0];
		component.m_randomShield = (GameObject[])(object)new GameObject[0];
		component2.m_name = "Egil";
		component2.m_health = 45f;
		component2.m_boss = false;
		component2.m_damageModifiers.m_blunt = (DamageModifier)0;
		component2.m_damageModifiers.m_slash = (DamageModifier)0;
		component2.m_damageModifiers.m_pierce = (DamageModifier)1;
		component2.m_damageModifiers.m_fire = (DamageModifier)0;
		component2.m_damageModifiers.m_frost = (DamageModifier)0;
		component2.m_damageModifiers.m_lightning = (DamageModifier)1;
		component2.m_damageModifiers.m_poison = (DamageModifier)3;
		component2.m_damageModifiers.m_spirit = (DamageModifier)0;
		component3.m_circleTargetDistance = 10.5f;
		component3.m_circleTargetInterval = 8f;
		component3.m_circleTargetDuration = 0.35f;
		DisableDrops(prefab);
		PrefabManager.Instance.AddPrefab(prefab);
		Logger.LogInfo((object)"Created Egil: normal skeleton body, Finewood Bow, lightning rapid-fire skirmisher behaviour.");
	}

	private void DisableDrops(GameObject prefab)
	{
		CharacterDrop component = prefab.GetComponent<CharacterDrop>();
		if ((Object)(object)component != (Object)null)
		{
			component.m_dropsEnabled = false;
		}
	}

	private GameObject[] ResolvePrefabs(params string[] prefabNames)
	{
		List<GameObject> list = new List<GameObject>();
		foreach (string text in prefabNames)
		{
			GameObject prefab = PrefabManager.Instance.GetPrefab(text);
			if ((Object)(object)prefab != (Object)null)
			{
				list.Add(prefab);
			}
			else
			{
				Logger.LogWarning((object)("Could not resolve prefab '" + text + "'."));
			}
		}
		return list.ToArray();
	}

	private void Update()
	{
		if ((Object)(object)Player.m_localPlayer == (Object)null)
		{
			ResetRuntimeStateForNoPlayer();
		}
		else
		{
			if (!_prefabsReady)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)_currentPlayer != (Object)(object)localPlayer)
			{
				InitializeForPlayer(localPlayer);
			}
			if (!_legacyMigrationDone && !TryMigrateLegacyEntourage(localPlayer))
			{
				return;
			}
			UpdateSummonDismissInput(localPlayer);
			if (_wardensDismissed)
			{
				if (_dismissCleanupNeeded)
				{
					EnsureAllWardensDismissed(localPlayer);
					_dismissCleanupNeeded = false;
				}
				return;
			}
			UpdateProgressionScaling(localPlayer, forceImmediate: false);
			UpdateCombatSense(localPlayer);
			UpdateInteriorTracking(localPlayer);
			UpdatePendingInteriorTransfer(localPlayer);
			WardenState[] wardens = _wardens;
			foreach (WardenState state in wardens)
			{
				UpdateWardenSlot(state, localPlayer);
			}
			RefreshFriendlyCollisionIgnores(localPlayer);
		}
	}

	private void ResetRuntimeStateForNoPlayer()
	{
		_currentPlayer = null;
		_legacyMigrationDone = false;
		_activeProgressionStage = -1;
		_progressionCheckTimer = 0f;
		_sharedThreat = null;
		_sharedThreatTimer = 0f;
		_combatSenseTimer = 0f;
		_wardensDismissed = false;
		_dismissCleanupNeeded = false;
		_isDismissOperation = false;
		_friendlyCollisionRefreshTimer = 0f;
		_hasInteriorState = false;
		_interiorTransferPending = false;
		_interiorTransferTimer = 0f;
		_postInteriorTransferGraceTimer = 0f;
		if (_wardens != null)
		{
			WardenState[] wardens = _wardens;
			foreach (WardenState wardenState in wardens)
			{
				ClearLoadedInstance(wardenState);
				wardenState.ReportedPersistentSearch = false;
			}
		}
	}

	private void InitializeForPlayer(Player player)
	{
		//IL_0169: Unknown result type (might be due to invalid IL or missing references)
		_currentPlayer = player;
		_legacyMigrationDone = false;
		long playerID = player.GetPlayerID();
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			ClearLoadedInstance(wardenState);
			wardenState.SpawnDelay = 3f;
			wardenState.StuckTime = 0f;
			wardenState.ReportedPersistentSearch = false;
			wardenState.PatrolTimer = 0f;
			wardenState.HasPatrolPoint = false;
			wardenState.WasInCombat = false;
			wardenState.SoftLeashIssued = false;
			wardenState.PersonalSpaceCooldown = 0f;
			wardenState.BlockHoldTimer = 0f;
			wardenState.CombatMoveTimer = 0f;
			wardenState.HasCombatMovePoint = false;
			if (wardenState.RespawnPending && wardenState.RespawnOwnerId != playerID)
			{
				wardenState.RespawnPending = false;
				wardenState.RespawnTimer = 0f;
				wardenState.RespawnOwnerId = 0L;
			}
		}
		_activeProgressionStage = -1;
		_progressionCheckTimer = 0f;
		_sharedThreat = null;
		_sharedThreatTimer = 0f;
		_combatSenseTimer = 0f;
		_friendlyCollisionRefreshTimer = 0f;
		_wardensDismissed = IsWardensDismissedForPlayer(player);
		_dismissCleanupNeeded = _wardensDismissed;
		if (!_wardensDismissed)
		{
			RestorePersistedRespawnStates(player);
		}
		UpdateProgressionScaling(player, forceImmediate: true);
		bool flag = Character.InInterior(((Component)player).transform.position);
		_hasInteriorState = true;
		_playerWasInInterior = flag;
		_pendingInteriorState = flag;
		_interiorTransferPending = false;
		_interiorTransferTimer = 0f;
		_interiorCheckTimer = 0f;
		_postInteriorTransferGraceTimer = 0f;
		Logger.LogInfo((object)(_wardensDismissed ? $"Player detected. Interior: {flag}. The Lyngvi Wardens are currently dismissed." : $"Player detected. Interior: {flag}. Preparing the Lyngvi Wardens..."));
	}

	private void UpdateSummonDismissInput(Player player)
	{
		if (!((Object)(object)player == (Object)null) && _summonDismissButton != null && ZInput.instance != null && ZInput.GetButtonDown(_summonDismissButton.Name))
		{
			ToggleWardens(player, showMessage: true);
		}
	}

	internal void HandleWardenConsoleCommand(string[] args, Terminal context)
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			if (context != null)
			{
				context.AddString("The Lyngvi Wardens: no local player is loaded.");
			}
			return;
		}
		if (!_prefabsReady)
		{
			if (context != null)
			{
				context.AddString("The Lyngvi Wardens: prefabs are not ready yet.");
			}
			return;
		}
		if (!_legacyMigrationDone && !TryMigrateLegacyEntourage(localPlayer))
		{
			if (context != null)
			{
				context.AddString("The Lyngvi Wardens: world data is not ready yet.");
			}
			return;
		}
		if (args == null || args.Length == 0)
		{
			if (context != null)
			{
				context.AddString("Usage: wardens <summon|dismiss|toggle|regroup>");
			}
			return;
		}
		switch (args[0].Trim().ToLowerInvariant())
		{
		case "summon":
			SummonWardens(localPlayer, showMessage: true);
			if (context != null)
			{
				context.AddString("The Lyngvi Wardens answer your call.");
			}
			break;
		case "dismiss":
			DismissWardens(localPlayer, showMessage: true);
			if (context != null)
			{
				context.AddString("The Lyngvi Wardens return to the mists.");
			}
			break;
		case "toggle":
			ToggleWardens(localPlayer, showMessage: true);
			if (context != null)
			{
				context.AddString(_wardensDismissed ? "The Lyngvi Wardens are dismissed." : "The Lyngvi Wardens are summoned.");
			}
			break;
		case "regroup":
			RegroupWardens(localPlayer, showMessage: true);
			if (context != null)
			{
				context.AddString(_wardensDismissed ? "The Lyngvi Wardens are currently dismissed." : "The Lyngvi Wardens regroup around you.");
			}
			break;
		default:
			if (context != null)
			{
				context.AddString("Usage: wardens <summon|dismiss|toggle|regroup>");
			}
			break;
		}
	}

	private void ToggleWardens(Player player, bool showMessage)
	{
		if (_wardensDismissed)
		{
			SummonWardens(player, showMessage);
		}
		else
		{
			DismissWardens(player, showMessage);
		}
	}

	private void DismissWardens(Player player, bool showMessage)
	{
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		if (_wardensDismissed)
		{
			if (showMessage)
			{
				ShowCenterMessage("The Lyngvi Wardens already rest in the mists.");
			}
			return;
		}
		SaveDismissSnapshot(player);
		_wardensDismissed = true;
		SetPlayerCustomData(player, "TheLyngviWardens.Dismissed", "1");
		EnsureAllWardensDismissed(player);
		_dismissCleanupNeeded = false;
		_sharedThreat = null;
		_sharedThreatTimer = 0f;
		if (showMessage)
		{
			ShowCenterMessage("The Lyngvi Wardens return to the mists.");
		}
		Logger.LogInfo((object)"The Lyngvi Wardens were dismissed.");
	}

	private void SummonWardens(Player player, bool showMessage)
	{
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		if (!_wardensDismissed)
		{
			if (showMessage)
			{
				ShowCenterMessage("The Lyngvi Wardens are already at your side.");
			}
			return;
		}
		_wardensDismissed = false;
		_dismissCleanupNeeded = false;
		RemovePlayerCustomData(player, "TheLyngviWardens.Dismissed");
		long playerID = player.GetPlayerID();
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			ClearLoadedInstance(wardenState);
			wardenState.RespawnPending = false;
			wardenState.RespawnTimer = 0f;
			wardenState.RespawnOwnerId = 0L;
			if (TryGetPlayerFloat(player, GetRespawnDataKey(wardenState.Slot), out var value) && value > 0f)
			{
				wardenState.RespawnPending = true;
				wardenState.RespawnTimer = Mathf.Clamp(value, 0.1f, 20f);
				wardenState.RespawnOwnerId = playerID;
			}
			else
			{
				wardenState.SpawnDelay = 0f;
			}
		}
		if (showMessage)
		{
			ShowCenterMessage("The Lyngvi Wardens answer your call.");
		}
		Logger.LogInfo((object)"The Lyngvi Wardens were summoned.");
	}

	private void RegroupWardens(Player player, bool showMessage)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: 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_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		if (_wardensDismissed)
		{
			if (showMessage)
			{
				ShowCenterMessage("The Lyngvi Wardens currently rest in the mists.");
			}
			return;
		}
		bool insideInterior = Character.InInterior(((Component)player).transform.position);
		long playerID = player.GetPlayerID();
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			if (wardenState.RespawnPending)
			{
				continue;
			}
			Vector3 returnPosition = GetReturnPosition(player, wardenState.Slot, insideInterior);
			if ((Object)(object)wardenState.Object != (Object)null)
			{
				TransferLoadedWarden(wardenState, player, returnPosition);
				continue;
			}
			ZDO val = FindSavedWardenZdo(wardenState, playerID, ((Component)player).transform.position);
			if (val != null)
			{
				val.SetPosition(returnPosition);
				val.Set(ZDOVars.s_follow, string.Empty);
				ZDOMan instance = ZDOMan.instance;
				if (instance != null)
				{
					instance.ForceSendZDO(val.m_uid);
				}
				wardenState.SpawnDelay = 0.25f;
			}
			else
			{
				wardenState.SpawnDelay = 0f;
			}
		}
		if (showMessage)
		{
			ShowCenterMessage("The Lyngvi Wardens regroup around you.");
		}
	}

	private void SaveDismissSnapshot(Player player)
	{
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			string healthDataKey = GetHealthDataKey(wardenState.Slot);
			string respawnDataKey = GetRespawnDataKey(wardenState.Slot);
			RemovePlayerCustomData(player, healthDataKey);
			RemovePlayerCustomData(player, respawnDataKey);
			if (wardenState.RespawnPending)
			{
				SetPlayerFloat(player, respawnDataKey, Mathf.Clamp(wardenState.RespawnTimer, 0.1f, 20f));
				continue;
			}
			Character val = (((Object)(object)wardenState.Object != (Object)null) ? wardenState.Object.GetComponent<Character>() : null);
			if ((Object)(object)val != (Object)null)
			{
				if (val.IsDead())
				{
					SetPlayerFloat(player, respawnDataKey, 20f);
					continue;
				}
				float maxHealth = val.GetMaxHealth();
				float health = val.GetHealth();
				if (maxHealth > 0f)
				{
					SetPlayerFloat(player, healthDataKey, Mathf.Clamp01(health / maxHealth));
					continue;
				}
			}
			SetPlayerFloat(player, healthDataKey, 1f);
		}
	}

	private void RestorePersistedRespawnStates(Player player)
	{
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		long playerID = player.GetPlayerID();
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			if (TryGetPlayerFloat(player, GetRespawnDataKey(wardenState.Slot), out var value) && !(value <= 0f))
			{
				wardenState.RespawnPending = true;
				wardenState.RespawnTimer = Mathf.Clamp(value, 0.1f, 20f);
				wardenState.RespawnOwnerId = playerID;
				wardenState.SpawnDelay = 0f;
			}
		}
	}

	private void RestoreStoredHealthPercentage(WardenState state, GameObject warden, Player player)
	{
		if (state == null || (Object)(object)warden == (Object)null || (Object)(object)player == (Object)null)
		{
			return;
		}
		string healthDataKey = GetHealthDataKey(state.Slot);
		if (!TryGetPlayerFloat(player, healthDataKey, out var value))
		{
			return;
		}
		Character component = warden.GetComponent<Character>();
		if ((Object)(object)component != (Object)null && !component.IsDead())
		{
			float maxHealth = component.GetMaxHealth();
			if (maxHealth > 0f)
			{
				component.SetHealth(Mathf.Max(1f, maxHealth * Mathf.Clamp01(value)));
			}
		}
		RemovePlayerCustomData(player, healthDataKey);
	}

	private void EnsureAllWardensDismissed(Player player)
	{
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		long playerID = player.GetPlayerID();
		_isDismissOperation = true;
		try
		{
			WardenState[] wardens = _wardens;
			foreach (WardenState wardenState in wardens)
			{
				bool flag = false;
				List<ZDO> candidateZdosForState = GetCandidateZdosForState(wardenState);
				foreach (ZDO item in candidateZdosForState)
				{
					if (!DoesZdoBelongToSlot(item, wardenState, playerID))
					{
						continue;
					}
					item.Set("SoulboundCompanion_IsSoulbound", false);
					try
					{
						GameObject val = (((Object)(object)ZNetScene.instance != (Object)null) ? ZNetScene.instance.FindInstance(item.m_uid) : null);
						if ((Object)(object)val != (Object)null && (Object)(object)ZNetScene.instance != (Object)null)
						{
							if ((Object)(object)wardenState.Object != (Object)null && (Object)(object)val == (Object)(object)wardenState.Object)
							{
								flag = true;
							}
							ZNetScene.instance.Destroy(val);
						}
						else if (ZDOMan.instance != null)
						{
							item.SetOwner(ZDOMan.GetSessionID());
							ZDOMan.instance.DestroyZDO(item);
						}
					}
					catch (Exception ex)
					{
						Logger.LogWarning((object)$"Could not dismiss {GetDisplayName(wardenState.Slot)} ZDO {item.m_uid}: {ex.Message}");
					}
				}
				if (!flag && (Object)(object)wardenState.Object != (Object)null)
				{
					ZNetView component = wardenState.Object.GetComponent<ZNetView>();
					if ((Object)(object)component != (Object)null && component.IsValid())
					{
						ZDO zDO = component.GetZDO();
						if (zDO != null)
						{
							zDO.Set("SoulboundCompanion_IsSoulbound", false);
						}
					}
					if ((Object)(object)ZNetScene.instance != (Object)null)
					{
						ZNetScene.instance.Destroy(wardenState.Object);
					}
					else
					{
						Object.Destroy((Object)(object)wardenState.Object);
					}
				}
				ClearLoadedInstance(wardenState);
				wardenState.RespawnPending = false;
				wardenState.RespawnTimer = 0f;
				wardenState.RespawnOwnerId = 0L;
			}
		}
		finally
		{
			_isDismissOperation = false;
		}
	}

	private static bool IsWardensDismissedForPlayer(Player player)
	{
		if ((Object)(object)player == (Object)null || player.m_customData == null)
		{
			return false;
		}
		string value;
		return player.m_customData.TryGetValue("TheLyngviWardens.Dismissed", out value) && value == "1";
	}

	private static string GetHealthDataKey(int slot)
	{
		return "TheLyngviWardens.Health." + slot.ToString(CultureInfo.InvariantCulture);
	}

	private static string GetRespawnDataKey(int slot)
	{
		return "TheLyngviWardens.Respawn." + slot.ToString(CultureInfo.InvariantCulture);
	}

	private static void SetPlayerCustomData(Player player, string key, string value)
	{
		if (!((Object)(object)player == (Object)null) && player.m_customData != null)
		{
			player.m_customData[key] = value;
		}
	}

	private static void RemovePlayerCustomData(Player player, string key)
	{
		if (!((Object)(object)player == (Object)null) && player.m_customData != null)
		{
			player.m_customData.Remove(key);
		}
	}

	private static void SetPlayerFloat(Player player, string key, float value)
	{
		SetPlayerCustomData(player, key, value.ToString("R", CultureInfo.InvariantCulture));
	}

	private static bool TryGetPlayerFloat(Player player, string key, out float value)
	{
		value = 0f;
		if ((Object)(object)player == (Object)null || player.m_customData == null)
		{
			return false;
		}
		if (!player.m_customData.TryGetValue(key, out var value2))
		{
			return false;
		}
		return float.TryParse(value2, NumberStyles.Float, CultureInfo.InvariantCulture, out value);
	}

	private static void ShowCenterMessage(string message)
	{
		if ((Object)(object)MessageHud.instance != (Object)null)
		{
			MessageHud.instance.ShowMessage((MessageType)2, message, 0, (Sprite)null, false, true);
		}
	}

	private void RefreshFriendlyCollisionIgnores(Player player)
	{
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		_friendlyCollisionRefreshTimer -= Time.deltaTime;
		if (_friendlyCollisionRefreshTimer > 0f)
		{
			return;
		}
		_friendlyCollisionRefreshTimer = 1f;
		Collider[] solidColliders = GetSolidColliders(((Component)player).gameObject);
		List<Collider[]> list = new List<Collider[]>();
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			if (!((Object)(object)wardenState.Object == (Object)null))
			{
				Collider[] solidColliders2 = GetSolidColliders(wardenState.Object);
				list.Add(solidColliders2);
				IgnoreColliderSets(solidColliders, solidColliders2);
			}
		}
		for (int j = 0; j < list.Count; j++)
		{
			for (int k = j + 1; k < list.Count; k++)
			{
				IgnoreColliderSets(list[j], list[k]);
			}
		}
	}

	private static Collider[] GetSolidColliders(GameObject obj)
	{
		if ((Object)(object)obj == (Object)null)
		{
			return (Collider[])(object)new Collider[0];
		}
		CapsuleCollider[] componentsInChildren = obj.GetComponentsInChildren<CapsuleCollider>(true);
		List<Collider> list = new List<Collider>();
		CapsuleCollider[] array = componentsInChildren;
		foreach (CapsuleCollider val in array)
		{
			if ((Object)(object)val != (Object)null && ((Collider)val).enabled && !((Collider)val).isTrigger)
			{
				list.Add((Collider)(object)val);
			}
		}
		return list.ToArray();
	}

	private static void IgnoreColliderSets(Collider[] first, Collider[] second)
	{
		if (first == null || second == null)
		{
			return;
		}
		foreach (Collider val in first)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			foreach (Collider val2 in second)
			{
				if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val == (Object)(object)val2))
				{
					Physics.IgnoreCollision(val, val2, true);
				}
			}
		}
	}

	private bool TryMigrateLegacyEntourage(Player player)
	{
		if ((Object)(object)player == (Object)null || ZDOMan.instance == null)
		{
			return false;
		}
		long playerID = player.GetPlayerID();
		int num = 0;
		num += RemoveLegacyPrefab("SoulboundCompanion_Warrior", playerID);
		num += RemoveLegacyPrefab("SoulboundCompanion_Archer", playerID);
		num += RemoveLegacyPrefab("Skeleton_NoArcher", playerID);
		num += RemoveLegacyPrefab("LyngviWarden_Bodvar", playerID);
		num += RemoveLegacyPrefab("LyngviWarden_Orvar_110", playerID);
		num += RemoveLegacyPrefab("LyngviWarden_Orvar", playerID);
		num += RemoveLegacyPrefab("LyngviWarden_Egil_Charred", playerID);
		num += RemoveLegacyPrefab("LyngviWarden_Egil", playerID);
		_legacyMigrationDone = true;
		if (num > 0)
		{
			Logger.LogInfo((object)$"Migrated from the old Soulbound Companion system. Removed {num} legacy companion(s).");
		}
		return true;
	}

	private int RemoveLegacyPrefab(string prefabName, long playerId)
	{
		//IL_0104: 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)
		int num = 0;
		List<ZDO> allZDOsWithPrefab = GetAllZDOsWithPrefab(prefabName);
		foreach (ZDO item in allZDOsWithPrefab)
		{
			if (item == null || !item.IsValid() || !item.GetBool("SoulboundCompanion_IsSoulbound", false) || item.GetLong("SoulboundCompanion_OwnerId", 0L) != playerId)
			{
				continue;
			}
			item.Set("SoulboundCompanion_IsSoulbound", false);
			try
			{
				GameObject val = null;
				if ((Object)(object)ZNetScene.instance != (Object)null)
				{
					val = ZNetScene.instance.FindInstance(item.m_uid);
				}
				if ((Object)(object)val != (Object)null && (Object)(object)ZNetScene.instance != (Object)null)
				{
					ZNetScene.instance.Destroy(val);
				}
				else
				{
					item.SetOwner(ZDOMan.GetSessionID());
					ZDOMan.instance.DestroyZDO(item);
				}
				num++;
			}
			catch (Exception ex)
			{
				Logger.LogWarning((object)$"Could not completely destroy legacy ZDO {item.m_uid}: {ex.Message}");
			}
		}
		return num;
	}

	private void UpdateProgressionScaling(Player player, bool forceImmediate)
	{
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		if (!forceImmediate)
		{
			_progressionCheckTimer -= Time.deltaTime;
			if (_progressionCheckTimer > 0f)
			{
				return;
			}
		}
		_progressionCheckTimer = 2f;
		ProgressionPower progressionPower = GetProgressionPower(player);
		if (forceImmediate || progressionPower.Stage != _activeProgressionStage)
		{
			_activeProgressionStage = progressionPower.Stage;
			Logger.LogInfo((object)$"Lyngvi Warden progression: {progressionPower.Name} | Level {progressionPower.Level} | Elemental bonus +{progressionPower.ElementalDamage:0}");
			ApplyProgressionScalingToLoadedWardens();
		}
	}

	private static ProgressionPower GetProgressionPower(Player player)
	{
		if ((Object)(object)player == (Object)null)
		{
			return GetProgressionPowerForStage(0);
		}
		List<string> uniqueKeys = player.GetUniqueKeys();
		HashSet<string> hashSet = ((uniqueKeys != null) ? new HashSet<string>(uniqueKeys) : new HashSet<string>());
		if (hashSet.Contains("defeated_fader"))
		{
			return GetProgressionPowerForStage(7);
		}
		if (hashSet.Contains("defeated_queen"))
		{
			return GetProgressionPowerForStage(6);
		}
		if (hashSet.Contains("defeated_goblinking"))
		{
			return GetProgressionPowerForStage(5);
		}
		if (hashSet.Contains("defeated_dragon"))
		{
			return GetProgressionPowerForStage(4);
		}
		if (hashSet.Contains("defeated_bonemass"))
		{
			return GetProgressionPowerForStage(3);
		}
		if (hashSet.Contains("defeated_gdking"))
		{
			return GetProgressionPowerForStage(2);
		}
		if (hashSet.Contains("defeated_eikthyr"))
		{
			return GetProgressionPowerForStage(1);
		}
		return GetProgressionPowerForStage(0);
	}

	private static ProgressionPower GetProgressionPowerForStage(int stage)
	{
		return stage switch
		{
			0 => new ProgressionPower(0, 2, 10f, "Meadows / Unproven"), 
			1 => new ProgressionPower(1, 5, 15f, "Black Forest / Eikthyr defeated"), 
			2 => new ProgressionPower(2, 7, 20f, "Swamp / Elder defeated"), 
			3 => new ProgressionPower(3, 9, 25f, "Mountain / Bonemass defeated"), 
			4 => new ProgressionPower(4, 11, 30f, "Plains / Moder defeated"), 
			5 => new ProgressionPower(5, 13, 35f, "Mistlands / Yagluth defeated"), 
			6 => new ProgressionPower(6, 15, 40f, "Ashlands / Queen defeated"), 
			_ => new ProgressionPower(7, 17, 45f, "Deep North / Fader defeated"), 
		};
	}

	private ProgressionPower GetActiveProgressionPower()
	{
		if (_activeProgressionStage < 0)
		{
			if ((Object)(object)Player.m_localPlayer != (Object)null)
			{
				return GetProgressionPower(Player.m_localPlayer);
			}
			return GetProgressionPowerForStage(0);
		}
		return GetProgressionPowerForStage(_activeProgressionStage);
	}

	private void ApplyProgressionScalingToLoadedWardens()
	{
		if (_wardens == null)
		{
			return;
		}
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			if (wardenState.Spawned && !((Object)(object)wardenState.Object == (Object)null))
			{
				ApplyProgressionPower(wardenState.Object, preserveHealthPercentage: true);
			}
		}
	}

	private void ApplyProgressionPower(GameObject warden, bool preserveHealthPercentage)
	{
		if ((Object)(object)warden == (Object)null)
		{
			return;
		}
		Character component = warden.GetComponent<Character>();
		if ((Object)(object)component == (Object)null || component.IsDead())
		{
			return;
		}
		ZNetView component2 = warden.GetComponent<ZNetView>();
		if ((Object)(object)component2 != (Object)null && component2.IsValid() && !component2.IsOwner())
		{
			LyngviWardenMarker component3 = warden.GetComponent<LyngviWardenMarker>();
			ZDO zDO = component2.GetZDO();
			if ((Object)(object)component3 != (Object)null && zDO != null)
			{
				component3.SetElementalDamage(zDO.GetFloat("SoulboundCompanion_ElementalDamage", 10f));
			}
			return;
		}
		ProgressionPower activeProgressionPower = GetActiveProgressionPower();
		float num = 1f;
		float maxHealth = component.GetMaxHealth();
		float health = component.GetHealth();
		if (preserveHealthPercentage && maxHealth > 0f && health > 0f)
		{
			num = Mathf.Clamp01(health / maxHealth);
		}
		component.SetLevel(activeProgressionPower.Level);
		float maxHealth2 = component.GetMaxHealth();
		if (preserveHealthPercentage)
		{
			component.SetHealth(Mathf.Max(1f, maxHealth2 * num));
		}
		else
		{
			component.SetHealth(maxHealth2);
		}
		LyngviWardenMarker component4 = warden.GetComponent<LyngviWardenMarker>();
		if ((Object)(object)component4 != (Object)null)
		{
			component4.SetElementalDamage(activeProgressionPower.ElementalDamage);
			if (component4.Slot == 0)
			{
				ApplyBodvarShieldPower(warden, activeProgressionPower.Stage);
			}
		}
		if ((Object)(object)component2 != (Object)null && component2.IsValid())
		{
			ZDO zDO2 = component2.GetZDO();
			if (zDO2 != null)
			{
				zDO2.Set("SoulboundCompanion_ScaledLevel", activeProgressionPower.Level);
				zDO2.Set("SoulboundCompanion_ElementalDamage", activeProgressionPower.ElementalDamage);
			}
		}
	}

	private static float GetBodvarShieldBlockPowerForStage(int stage)
	{
		return stage switch
		{
			0 => 60f, 
			1 => 90f, 
			2 => 120f, 
			3 => 150f, 
			4 => 180f, 
			5 => 210f, 
			6 => 240f, 
			_ => 270f, 
		};
	}

	private void ApplyBodvarShieldPower(GameObject warden, int stage)
	{
		float bodvarShieldBlockPowerForStage = GetBodvarShieldBlockPowerForStage(stage);
		GameObject prefab = PrefabManager.Instance.GetPrefab("LyngviWarden_BodvarShield");
		ItemDrop val = (((Object)(object)prefab != (Object)null) ? prefab.GetComponent<ItemDrop>() : null);
		if ((Object)(object)val != (Object)null && val.m_itemData != null && val.m_itemData.m_shared != null)
		{
			val.m_itemData.m_shared.m_blockPower = bodvarShieldBlockPowerForStage;
			val.m_itemData.m_shared.m_blockPowerPerLevel = 0f;
			val.m_itemData.m_shared.m_timedBlockBonus = 1f;
		}
		Humanoid humanoid = (((Object)(object)warden != (Object)null) ? warden.GetComponent<Humanoid>() : null);
		ItemData val2 = TryGetCurrentBlocker(humanoid);
		if (val2 != null && val2.m_shared != null && (Object)(object)val2.m_dropPrefab != (Object)null && ((Object)val2.m_dropPrefab).name.StartsWith("LyngviWarden_BodvarShield", StringComparison.Ordinal))
		{
			val2.m_shared.m_blockPower = bodvarShieldBlockPowerForStage;
			val2.m_shared.m_blockPowerPerLevel = 0f;
			val2.m_shared.m_timedBlockBonus = 1f;
		}
	}

	private static ItemData TryGetCurrentBlocker(Humanoid humanoid)
	{
		if ((Object)(object)humanoid == (Object)null || HumanoidGetCurrentBlockerMethod == null)
		{
			return null;
		}
		try
		{
			object? obj = HumanoidGetCurrentBlockerMethod.Invoke(humanoid, null);
			return (ItemData)((obj is ItemData) ? obj : null);
		}
		catch
		{
			return null;
		}
	}

	private void UpdateInteriorTracking(Player player)
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		_interiorCheckTimer -= Time.deltaTime;
		if (!(_interiorCheckTimer > 0f))
		{
			_interiorCheckTimer = 0.25f;
			bool flag = Character.InInterior(((Component)player).transform.position);
			if (!_hasInteriorState)
			{
				_hasInteriorState = true;
				_playerWasInInterior = flag;
			}
			else if (flag != _playerWasInInterior)
			{
				_playerWasInInterior = flag;
				_pendingInteriorState = flag;
				_interiorTransferPending = true;
				_interiorTransferTimer = 0.6f;
				Logger.LogInfo((object)(flag ? "Player entered an interior. Lyngvi Wardens transfer scheduled." : "Player left an interior. Lyngvi Wardens transfer scheduled."));
			}
			else if (!_interiorTransferPending)
			{
				EnsureLoadedWardensMatchInterior(player, flag);
			}
		}
	}

	private void UpdatePendingInteriorTransfer(Player player)
	{
		if (_postInteriorTransferGraceTimer > 0f)
		{
			_postInteriorTransferGraceTimer -= Time.deltaTime;
		}
		if (_interiorTransferPending)
		{
			_interiorTransferTimer -= Time.deltaTime;
			if (!(_interiorTransferTimer > 0f))
			{
				_interiorTransferPending = false;
				_interiorTransferTimer = 0f;
				TransferEntireEntourageAcrossInteriorBoundary(player, _pendingInteriorState);
				_postInteriorTransferGraceTimer = 2f;
			}
		}
	}

	private void EnsureLoadedWardensMatchInterior(Player player, bool playerInside)
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			if (!wardenState.RespawnPending && !((Object)(object)wardenState.Object == (Object)null))
			{
				bool flag = Character.InInterior(wardenState.Object.transform.position);
				if (flag != playerInside)
				{
					Logger.LogInfo((object)(GetDisplayName(wardenState.Slot) + " is on the wrong side of an interior boundary. Correcting..."));
					Vector3 returnPosition = GetReturnPosition(player, wardenState.Slot, playerInside);
					TransferLoadedWarden(wardenState, player, returnPosition);
				}
			}
		}
	}

	private void TransferEntireEntourageAcrossInteriorBoundary(Player player, bool targetInside)
	{
		//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_008c: 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_00dc: 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_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		long playerID = player.GetPlayerID();
		int num = 0;
		int num2 = 0;
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			if (wardenState.RespawnPending)
			{
				continue;
			}
			Vector3 returnPosition = GetReturnPosition(player, wardenState.Slot, targetInside);
			if ((Object)(object)wardenState.Object != (Object)null)
			{
				TransferLoadedWarden(wardenState, player, returnPosition);
				num++;
				continue;
			}
			ZDO val = FindSavedWardenZdo(wardenState, playerID, ((Component)player).transform.position);
			if (val == null)
			{
				wardenState.SpawnDelay = 0.5f;
				continue;
			}
			StampWardenIdentity(val, wardenState, player);
			GameObject val2 = null;
			if ((Object)(object)ZNetScene.instance != (Object)null)
			{
				val2 = ZNetScene.instance.FindInstance(val.m_uid);
			}
			if ((Object)(object)val2 != (Object)null)
			{
				AdoptWarden(wardenState, val2, player);
				TransferLoadedWarden(wardenState, player, returnPosition);
				num++;
				continue;
			}
			val.SetPosition(returnPosition);
			val.Set(ZDOVars.s_follow, string.Empty);
			ZDOMan instance = ZDOMan.instance;
			if (instance != null)
			{
				instance.ForceSendZDO(val.m_uid);
			}
			wardenState.SpawnDelay = 0.5f;
			wardenState.ReportedPersistentSearch = false;
			num2++;
		}
		Logger.LogInfo((object)$"Lyngvi Wardens interior transfer completed. Loaded moved: {num}, saved/unloaded moved: {num2}.");
	}

	private void TransferLoadedWarden(WardenState state, Player player, Vector3 targetPosition)
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)state.Object == (Object)null || (Object)(object)player == (Object)null)
		{
			return;
		}
		state.Object.transform.position = targetPosition;
		state.Object.transform.rotation = ((Component)player).transform.rotation;
		ZNetView component = state.Object.GetComponent<ZNetView>();
		if ((Object)(object)component != (Object)null && component.IsValid())
		{
			ZDO zDO = component.GetZDO();
			if (zDO != null)
			{
				StampWardenIdentity(zDO, state, player);
				zDO.SetPosition(targetPosition);
				zDO.Set(ZDOVars.s_follow, string.Empty);
				ZDOMan instance = ZDOMan.instance;
				if (instance != null)
				{
					instance.ForceSendZDO(zDO.m_uid);
				}
			}
		}
		if ((Object)(object)state.AI == (Object)null)
		{
			state.AI = state.Object.GetComponent<MonsterAI>();
		}
		if ((Object)(object)state.AI != (Object)null)
		{
			((Behaviour)state.AI).enabled = true;
		}
		LyngviWardenMarker component2 = state.Object.GetComponent<LyngviWardenMarker>();
		if ((Object)(object)component2 != (Object)null)
		{
			component2.ApplyInteriorCollisionProfile(Character.InInterior(state.Object.transform.position));
		}
		state.LastPosition = targetPosition;
		state.StuckTime = 0f;
		state.MovementCheckTimer = 1f;
		state.SoftLeashIssued = false;
		AssignNewPatrolPoint(state, player, immediate: true);
	}

	internal void ObserveCombatHit(Character victim, Character attacker)
	{
		Player localPlayer = Player.m_localPlayer;
		if (!((Object)(object)localPlayer == (Object)null) && !((Object)(object)victim == (Object)null) && !((Object)(object)attacker == (Object)null))
		{
			if ((Object)(object)attacker == (Object)(object)localPlayer && IsValidThreat(victim, localPlayer))
			{
				SetSharedThreat(victim, localPlayer);
			}
			else if ((Object)(object)victim == (Object)(object)localPlayer && IsValidThreat(attacker, localPlayer))
			{
				SetSharedThreat(attacker, localPlayer);
			}
			else if (IsOwnedWarden(victim) && IsValidThreat(attacker, localPlayer))
			{
				SetSharedThreat(attacker, localPlayer);
			}
			else if (IsOwnedWarden(attacker) && IsValidThreat(victim, localPlayer))
			{
				SetSharedThreat(victim, localPlayer);
			}
		}
	}

	private bool IsOwnedWarden(Character character)
	{
		if ((Object)(object)character == (Object)null)
		{
			return false;
		}
		LyngviWardenMarker component = ((Component)character).GetComponent<LyngviWardenMarker>();
		if ((Object)(object)component == (Object)null)
		{
			return false;
		}
		ZNetView component2 = ((Component)character).GetComponent<ZNetView>();
		ZDO val = (((Object)(object)component2 != (Object)null && component2.IsValid()) ? component2.GetZDO() : null);
		Player localPlayer = Player.m_localPlayer;
		return val != null && (Object)(object)localPlayer != (Object)null && val.GetBool("SoulboundCompanion_IsSoulbound", false) && val.GetLong("SoulboundCompanion_OwnerId", 0L) == localPlayer.GetPlayerID();
	}

	private static bool IsValidThreat(Character target, Player player)
	{
		if ((Object)(object)target == (Object)null || (Object)(object)player == (Object)null || (Object)(object)target == (Object)(object)player || target.IsDead())
		{
			return false;
		}
		return BaseAI.IsEnemy((Character)(object)player, target);
	}

	private void SetSharedThreat(Character target, Player player)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		if (IsValidThreat(target, player))
		{
			float num = Vector3.Distance(((Component)player).transform.position, ((Component)target).transform.position);
			if (!(num > 45f))
			{
				_sharedThreat = target;
				_sharedThreatTimer = 6f;
			}
		}
	}

	private void UpdateCombatSense(Player player)
	{
		//IL_006f: 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_012e: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		if (_sharedThreatTimer > 0f)
		{
			_sharedThreatTimer -= Time.deltaTime;
		}
		if ((Object)(object)_sharedThreat != (Object)null && (_sharedThreatTimer <= 0f || !IsValidThreat(_sharedThreat, player) || Vector3.Distance(((Component)player).transform.position, ((Component)_sharedThreat).transform.position) > 45f))
		{
			_sharedThreat = null;
			_sharedThreatTimer = 0f;
		}
		_combatSenseTimer -= Time.deltaTime;
		if (_combatSenseTimer > 0f)
		{
			return;
		}
		_combatSenseTimer = 0.15f;
		Character frontlineTarget = GetFrontlineTarget(player);
		if ((Object)(object)_sharedThreat == (Object)null && (Object)(object)frontlineTarget != (Object)null)
		{
			SetSharedThreat(frontlineTarget, player);
		}
		if ((Object)(object)_sharedThreat == (Object)null)
		{
			Character val = FindClosestAlertedEnemy(((Component)player).transform.position, 45f, player);
			if ((Object)(object)val != (Object)null)
			{
				SetSharedThreat(val, player);
			}
		}
		WardenState state = GetState(2);
		if (state != null && state.Spawned && (Object)(object)state.Object != (Object)null && !state.RespawnPending && (Object)(object)_sharedThreat != (Object)null)
		{
			AlertWardenToTarget(state, _sharedThreat, player);
		}
		WardenState state2 = GetState(3);
		if (state2 != null && state2.Spawned && !((Object)(object)state2.Object == (Object)null) && !state2.RespawnPending)
		{
			Character val2 = ChooseEgilTarget(state2, player);
			if ((Object)(object)val2 != (Object)null)
			{
				AlertWardenToTarget(state2, val2, player);
			}
		}
	}

	private Character GetFrontlineTarget(Player player)
	{
		int[] array = new int[2] { 0, 1 };
		int[] array2 = array;
		foreach (int slot in array2)
		{
			WardenState state = GetState(slot);
			Character stateTarget = GetStateTarget(state);
			if (IsValidThreat(stateTarget, player))
			{
				return stateTarget;
			}
		}
		return null;
	}

	private Character ChooseEgilTarget(WardenState egil, Player player)
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		WardenState state = GetState(2);
		if (state != null && state.Spawned && (Object)(object)state.Object != (Object)null)
		{
			Character val = FindClosestAlertedEnemy(state.Object.transform.position, 13f, player);
			if ((Object)(object)val != (Object)null)
			{
				return val;
			}
		}
		Character val2 = FindClosestAlertedEnemy(((Component)player).transform.position, 13f, player);
		if ((Object)(object)val2 != (Object)null)
		{
			return val2;
		}
		if ((Object)(object)_sharedThreat != (Object)null && IsValidThreat(_sharedThreat, player))
		{
			return _sharedThreat;
		}
		return FindClosestAlertedEnemy(egil.Object.transform.position, 32f, player);
	}

	private static Character FindClosestAlertedEnemy(Vector3 center, float radius, Player player)
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return null;
		}
		List<Character> list = new List<Character>();
		Character.GetCharactersInRange(center, radius, list);
		Character result = null;
		float num = float.MaxValue;
		foreach (Character item in list)
		{
			if (!IsValidThreat(item, player))
			{
				continue;
			}
			BaseAI baseAI = item.GetBaseAI();
			if (!((Object)(object)baseAI == (Object)null) && baseAI.IsAlerted())
			{
				float num2 = Vector3.Distance(center, ((Component)item).transform.position);
				if (num2 < num)
				{
					num = num2;
					result = item;
				}
			}
		}
		return result;
	}

	private static Character GetStateTarget(WardenState state)
	{
		if (state == null || (Object)(object)state.AI == (Object)null)
		{
			return null;
		}
		return ((BaseAI)state.AI).GetTargetCreature();
	}

	private static void AlertWardenToTarget(WardenState state, Character target, Player player)
	{
		if (state != null && !((Object)(object)state.AI == (Object)null) && !((Object)(object)target == (Object)null) && IsValidThreat(target, player))
		{
			state.AI.SetTarget(target);
			((BaseAI)state.AI).SetAlerted(true);
		}
	}

	private void UpdateSpecialCombatBehaviour(MonsterAI ai, float dt)
	{
		if ((Object)(object)ai == (Object)null || _wardens == null)
		{
			return;
		}
		WardenState wardenState = null;
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState2 in wardens)
		{
			if ((Object)(object)wardenState2.AI == (Object)(object)ai)
			{
				wardenState = wardenState2;
				break;
			}
		}
		if (wardenState == null || (Object)(object)wardenState.Object == (Object)null || !wardenState.Spawned || wardenState.RespawnPending)
		{
			return;
		}
		ZNetView component = wardenState.Object.GetComponent<ZNetView>();
		if (!((Object)(object)component != (Object)null) || !component.IsValid() || component.IsOwner())
		{
			switch (wardenState.Slot)
			{
			case 0:
				UpdateBodvarBlocking(wardenState, dt);
				break;
			case 2:
				UpdateOrvarKiting(wardenState, dt);
				break;
			case 3:
				UpdateEgilSkirmishing(wardenState, dt);
				break;
			case 1:
				break;
			}
		}
	}

	private void UpdateBodvarBlocking(WardenState state, float dt)
	{
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		Character val = (((Object)(object)state.Object != (Object)null) ? state.Object.GetComponent<Character>() : null);
		Humanoid val2 = (((Object)(object)state.Object != (Object)null) ? state.Object.GetComponent<Humanoid>() : null);
		Character stateTarget = GetStateTarget(state);
		if (!((Object)(object)val == (Object)null) && !((Object)(object)val2 == (Object)null))
		{
			state.BlockHoldTimer = Mathf.Max(0f, state.BlockHoldTimer - dt);
			if ((Object)(object)stateTarget != (Object)null && !stateTarget.IsDead() && Vector3.Distance(((Component)val).transform.position, ((Component)stateTarget).transform.position) <= 7f && stateTarget.InAttack() && !val.InAttack())
			{
				state.BlockHoldTimer = 0.65f;
				TryLookAt((BaseAI)(object)state.AI, stateTarget.GetCenterPoint());
			}
			bool flag = state.BlockHoldTimer > 0f && (Object)(object)stateTarget != (Object)null && !stateTarget.IsDead() && !val.InAttack();
			TrySetBlocking(val, flag);
			if (flag)
			{
				TrySetBlockTimer(val2, 0.35f);
			}
		}
	}

	private void UpdateOrvarKiting(WardenState state, float dt)
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		Character stateTarget = GetStateTarget(state);
		if ((Object)(object)stateTarget == (Object)null || stateTarget.IsDead() || (Object)(object)state.Object == (Object)null || (Object)(object)state.AI == (Object)null)
		{
			state.HasCombatMovePoint = false;
			return;
		}
		Character component = state.Object.GetComponent<Character>();
		if (!((Object)(object)component != (Object)null) || !component.InAttack())
		{
			float num = Vector3.Distance(state.Object.transform.position, ((Component)stateTarget).transform.position);
			if (num >= 13f)
			{
				state.HasCombatMovePoint = false;
				return;
			}
			float distance = ((num <= 8f) ? 19f : 16f);
			Vector3 point = BuildRetreatPoint(state.Object.transform.position, ((Component)stateTarget).transform.position, distance);
			state.CombatMovePoint = GroundCombatPoint(point, state.Object.transform.position.y, Character.InInterior(state.Object.transform.position));
			state.HasCombatMovePoint = true;
			TryMoveTo((BaseAI)(object)state.AI, dt, state.CombatMovePoint, 1.5f, run: true);
		}
	}

	private void UpdateEgilSkirmishing(WardenState state, float dt)
	{
		//IL_0089: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		Character stateTarget = GetStateTarget(state);
		if ((Object)(object)stateTarget == (Object)null || stateTarget.IsDead() || (Object)(object)state.Object == (Object)null || (Object)(object)state.AI == (Object)null)
		{
			state.CombatMoveTimer = 0f;
			state.HasCombatMovePoint = false;
			return;
		}
		Character component = state.Object.GetComponent<Character>();
		if (!((Object)(object)component != (Object)null) || !component.InAttack())
		{
			float num = Vector3.Distance(state.Object.transform.position, ((Component)stateTarget).transform.position);
			if (num >= 6.5f)
			{
				state.HasCombatMovePoint = false;
				state.CombatMoveTimer = 0f;
				return;
			}
			Vector3 point = BuildRetreatPoint(state.Object.transform.position, ((Component)stateTarget).transform.position, 10.5f);
			state.CombatMovePoint = GroundCombatPoint(point, state.Object.transform.position.y, Character.InInterior(state.Object.transform.position));
			state.HasCombatMovePoint = true;
			TryMoveTo((BaseAI)(object)state.AI, dt, state.CombatMovePoint, 1.25f, run: true);
		}
	}

	private static Vector3 BuildRetreatPoint(Vector3 wardenPosition, Vector3 enemyPosition, float distance)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: 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)
		Vector3 val = wardenPosition - enemyPosition;
		val.y = 0f;
		if (((Vector3)(ref val)).sqrMagnitude < 0.01f)
		{
			val = Vector3.back;
		}
		((Vector3)(ref val)).Normalize();
		return enemyPosition + val * distance;
	}

	private static Vector3 BuildFlankPoint(Vector3 enemyPosition, Vector3 wardenPosition, float direction, float radius)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		Vector3 val = wardenPosition - enemyPosition;
		val.y = 0f;
		if (((Vector3)(ref val)).sqrMagnitude < 0.01f)
		{
			val = Vector3.back;
		}
		((Vector3)(ref val)).Normalize();
		Vector3 val2 = Quaternion.AngleAxis(55f * direction, Vector3.up) * val;
		return enemyPosition + ((Vector3)(ref val2)).normalized * radius;
	}

	private static Vector3 GroundCombatPoint(Vector3 point, float fallbackY, bool insideInterior)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		if (insideInterior)
		{
			point.y = fallbackY;
			return point;
		}
		RaycastHit val = default(RaycastHit);
		if (Physics.Raycast(point + Vector3.up * 5f, Vector3.down, ref val, 15f))
		{
			return ((RaycastHit)(ref val)).point + Vector3.up * 0.2f;
		}
		point.y = fallbackY;
		return point;
	}

	private static bool TryMoveTo(BaseAI ai, float dt, Vector3 point, float distance, bool run)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)ai == (Object)null || BaseAIMoveToMethod == null)
		{
			return false;
		}
		try
		{
			object obj = BaseAIMoveToMethod.Invoke(ai, new object[4] { dt, point, distance, run });
			return obj is bool && (bool)obj;
		}
		catch
		{
			return false;
		}
	}

	private static void TryLookAt(BaseAI ai, Vector3 point)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)ai == (Object)null || BaseAILookAtMethod == null)
		{
			return;
		}
		try
		{
			BaseAILookAtMethod.Invoke(ai, new object[1] { point });
		}
		catch
		{
		}
	}

	private static void TrySetBlocking(Character character, bool blocking)
	{
		if ((Object)(object)character == (Object)null || CharacterBlockingField == null)
		{
			return;
		}
		try
		{
			CharacterBlockingField.SetValue(character, blocking);
		}
		catch
		{
		}
	}

	private static void TrySetBlockTimer(Humanoid humanoid, float value)
	{
		if ((Object)(object)humanoid == (Object)null || HumanoidBlockTimerField == null)
		{
			return;
		}
		try
		{
			HumanoidBlockTimerField.SetValue(humanoid, value);
		}
		catch
		{
		}
	}

	private bool IsWardenInCombat(WardenState state)
	{
		if (state == null || (Object)(object)state.AI == (Object)null)
		{
			return false;
		}
		BaseAI aI = (BaseAI)(object)state.AI;
		return aI.IsAlerted() || aI.HaveTarget();
	}

	private void AssignNewPatrolPoint(WardenState state, Player player, bool immediate)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		if (state == null || (Object)(object)state.Object == (Object)null || (Object)(object)state.AI == (Object)null || (Object)(object)player == (Object)null)
		{
			return;
		}
		bool insideInterior = Character.InInterior(((Component)player).transform.position);
		Vector3 patrolPosition = GetPatrolPosition(player, state.Slot, insideInterior);
		state.AI.SetFollowTarget((GameObject)null);
		BaseAI aI = (BaseAI)(object)state.AI;
		aI.ResetPatrolPoint();
		aI.SetPatrolPoint(patrolPosition);
		ZNetView component = state.Object.GetComponent<ZNetView>();
		if ((Object)(object)component != (Object)null && component.IsValid())
		{
			ZDO zDO = component.GetZDO();
			if (zDO != null)
			{
				zDO.Set(ZDOVars.s_follow, string.Empty);
			}
		}
		state.PatrolAnchor = ((Component)player).transform.position;
		state.HasPatrolPoint = true;
		state.PatrolTimer = (immediate ? Random.Range(4f, 7f) : Random.Range(8f, 14f));
	}

	private void UpdatePeacefulPatrol(WardenState state, Player player, float distanceFromPlayer)
	{
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_026f: 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_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)
		if (state == null || (Object)(object)state.Object == (Object)null || (Object)(object)state.AI == (Object)null || (Object)(object)player == (Object)null)
		{
			return;
		}
		if (IsWardenInCombat(state))
		{
			state.WasInCombat = true;
			state.StuckTime = 0f;
			return;
		}
		if (state.PersonalSpaceCooldown > 0f)
		{
			state.PersonalSpaceCooldown -= Time.deltaTime;
		}
		if (state.WasInCombat)
		{
			state.WasInCombat = false;
			state.PatrolTimer = 0f;
			state.SoftLeashIssued = false;
		}
		bool flag = Character.InInterior(((Component)player).transform.position);
		if (!flag && state.Slot == 0 && distanceFromPlayer < 8.5f && state.PersonalSpaceCooldown <= 0f)
		{
			AssignNewPatrolPoint(state, player, immediate: true);
			state.PersonalSpaceCooldown = 2.5f;
			return;
		}
		if (distanceFromPlayer <= 24f)
		{
			state.SoftLeashIssued = false;
		}
		if (distanceFromPlayer > 30f && !state.SoftLeashIssued)
		{
			state.SoftLeashIssued = true;
			AssignNewPatrolPoint(state, player, immediate: true);
			return;
		}
		state.MovementCheckTimer -= Time.deltaTime;
		if (state.MovementCheckTimer <= 0f)
		{
			state.MovementCheckTimer = 1f;
			float num = Vector3.Distance(state.Object.transform.position, state.LastPosition);
			float num2 = (flag ? 7.5f : 32f);
			float num3 = (flag ? 0.2f : 0.35f);
			float num4 = (flag ? 3f : 8f);
			if (distanceFromPlayer >= num2 && num < num3)
			{
				state.StuckTime += 1f;
			}
			else
			{
				state.StuckTime = 0f;
			}
			state.LastPosition = state.Object.transform.position;
			if (state.StuckTime >= num4)
			{
				CatchUpToPlayer(state, player, flag ? "stuck behind interior geometry" : "stuck far from player");
				return;
			}
		}
		state.PatrolTimer -= Time.deltaTime;
		bool flag2 = !state.HasPatrolPoint || Vector3.Distance(((Component)player).transform.position, state.PatrolAnchor) >= 12f;
		if (state.PatrolTimer <= 0f || flag2)
		{
			AssignNewPatrolPoint(state, player, immediate: false);
		}
	}

	private static Vector3 GetPatrolPosition(Player player, int slot, bool insideInterior)
	{
		//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_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: 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_0160: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: 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_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: 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_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01da: Unknown result type (might be due to invalid IL or missing references)
		//IL_0204: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fe: 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)
		float num;
		float num2;
		if (insideInterior)
		{
			switch (slot)
			{
			case 2:
				num = 4.5f;
				num2 = 6.5f;
				break;
			case 3:
				num = 4f;
				num2 = 6f;
				break;
			default:
				num = 3f;
				num2 = 5f;
				break;
			}
		}
		else
		{
			switch (slot)
			{
			case 0:
				num = 12f;
				num2 = 18f;
				break;
			case 1:
				num = 8f;
				num2 = 14f;
				break;
			case 2:
				num = 12f;
				num2 = 18f;
				break;
			case 3:
				num = 10f;
				num2 = 16f;
				break;
			default:
				num = 8f;
				num2 = 14f;
				break;
			}
		}
		float num3 = Random.Range(num, num2);
		float num4 = slot switch
		{
			0 => -75f, 
			1 => 75f, 
			2 => 155f, 
			3 => -155f, 
			_ => 180f, 
		} + Random.Range(-55f, 55f);
		Vector3 val = Quaternion.AngleAxis(num4, Vector3.up) * ((Component)player).transform.forward;
		val.y = 0f;
		((Vector3)(ref val)).Normalize();
		Vector3 val2 = ((Component)player).transform.position + val * num3;
		if (insideInterior)
		{
			val2.y = ((Component)player).transform.position.y + 0.1f;
			return val2;
		}
		Vector3 val3 = val2 + Vector3.up * 5f;
		RaycastHit val4 = default(RaycastHit);
		if (Physics.Raycast(val3, Vector3.down, ref val4, 15f))
		{
			val2 = ((RaycastHit)(ref val4)).point + Vector3.up * 0.2f;
		}
		else
		{
			val2.y = ((Component)player).transform.position.y + 0.5f;
		}
		return val2;
	}

	private void UpdateWardenSlot(WardenState state, Player player)
	{
		if (_interiorTransferPending)
		{
			return;
		}
		if (state.RespawnPending)
		{
			state.RespawnTimer -= Time.deltaTime;
			if (!(state.RespawnTimer > 0f))
			{
				state.RespawnPending = false;
				state.RespawnTimer = 0f;
				state.RespawnOwnerId = 0L;
				Logger.LogInfo((object)(GetDisplayName(state.Slot) + "'s soul has reformed."));
				SpawnNewWarden(state, player);
			}
		}
		else if (state.Spawned && (Object)(object)state.Object == (Object)null)
		{
			ClearLoadedInstance(state);
			state.SpawnDelay = 0.5f;
		}
		else if (state.Spawned)
		{
			UpdateWardenMovement(state, player);
		}
		else
		{
			state.SpawnDelay -= Time.deltaTime;
			if (!(state.SpawnDelay > 0f) && !TryReconnectExistingWarden(state, player))
			{
				SpawnNewWarden(state, player);
			}
		}
	}

	internal void ScheduleRespawn(int slot, long ownerId)
	{
		WardenState state = GetState(slot);
		if (state != null && !state.RespawnPending)
		{
			state.RespawnPending = true;
			state.RespawnTimer = 20f;
			state.RespawnOwnerId = ownerId;
			ClearLoadedInstance(state);
			Logger.LogInfo((object)$"{GetDisplayName(slot)} was destroyed. Soul reforming in {20f} seconds.");
		}
	}

	private WardenState GetState(int slot)
	{
		if (_wardens == null)
		{
			return null;
		}
		WardenState[] wardens = _wardens;
		foreach (WardenState wardenState in wardens)
		{
			if (wardenState.Slot == slot)
			{
				return wardenState;
			}
		}
		return null;
	}

	private static string GetPrefabNameForSlot(int slot)
	{
		return slot switch
		{
			0 => "LyngviWarden_Bodvar_110", 
			1 => "LyngviWarden_Starkad", 
			2 => "LyngviWarden_Orvar_120", 
			3 => "LyngviWarden_Egil_120", 
			_ => "LyngviWarden_Bodvar_110", 
		};
	}

	private List<ZDO> GetCandidateZdosForState(WardenState state)
	{
		return GetAllZDOsWithPrefab(GetPrefabNameForSlot(state.Slot));
	}

	private ZDO FindSavedWardenZdo(WardenState state, long playerId, Vector3 referencePosition)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		List<ZDO> candidateZdosForState = GetCandidateZdosForState(state);
		ZDO result = null;
		float num = float.MaxValue;
		foreach (ZDO item in candidateZdosForState)
		{
			if (DoesZdoBelongToSlot(item, state, playerId))
			{
				float num2 = Vector3.Distance(item.GetPosition(), referencePosition);
				if (num2 < num)
				{
					num = num2;
					result = item;
				}
			}
		}
		return result;
	}

	private bool TryReconnectExistingWarden(WardenState state, Player player)
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0246: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02be: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0273: 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_0296: Unknown result type (might be due to invalid IL or missing references)
		//IL_029b: 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_0137: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: 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)
		if ((Object)(object)player == (Object)null || ZDOMan.instance == null || (Object)(object)ZNetScene.instance == (Object)null)
		{
			state.SpawnDelay = 1f;
			return true;
		}
		long playerID = player.GetPlayerID();
		bool flag = Character.InInterior(((Component)player).transform.position);
		LyngviWardenMarker[] array = Object.FindObjectsByType<LyngviWardenMarker>((FindObjectsSortMode)0);
		LyngviWardenMarker[] array2 = array;
		foreach (LyngviWardenMarker lyngviWardenMarker in array2)
		{
			if ((Object)(object)lyngviWardenMarker == (Object)null)
			{
				continue;
			}
			ZNetView component = ((Component)lyngviWardenMarker).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				continue;
			}
			ZDO zDO = component.GetZDO();
			if (!DoesZdoBelongToSlot(zDO, state, playerID))
			{
				continue;
			}
			Character component2 = ((Component)lyngviWardenMarker).GetComponent<Character>();
			if (!((Object)(object)component2 != (Object)null) || !component2.IsDead())
			{
				StampWardenIdentity(zDO, state, player);
				AdoptWarden(state, ((Component)lyngviWardenMarker).gameObject, player);
				bool flag2 = Character.InInterior(((Component)lyngviWardenMarker).transform.position);
				if (flag2 != flag)
				{
					Vector3 returnPosition = GetReturnPosition(player, state.Slot, flag);
					TransferLoadedWarden(state, player, returnPosition);
				}
				return true;
			}
		}
		List<ZDO> candidateZdosForState = GetCandidateZdosForState(state);
		ZDO val = null;
		int num = 0;
		float num2 = float.MaxValue;
		foreach (ZDO item in candidateZdosForState)
		{
			if (DoesZdoBelongToSlot(item, state, playerID))
			{
				num++;
				float num3 = Vector3.Distance(item.GetPosition(), ((Component)player).transform.position);
				if (num3 < num2)
				{
					num2 = num3;
					val = item;
				}
			}
		}
		if (num > 1)
		{
			Logger.LogWarning((object)$"Found {num} saved copies of {GetDisplayName(state.Slot)}. Using the closest.");
		}
		if (val == null)
		{
			return false;
		}
		StampWardenIdentity(val, state, player);
		GameObject val2 = ZNetScene.instance.FindInstance(val.m_uid);
		if ((Object)(object)val2 != (Object)null)
		{
			AdoptWarden(state, val2, player);
			bool flag3 = Character.InInterior(val2.transform.position);
			if (flag3 != flag)
			{
				Vector3 returnPosition2 = GetReturnPosition(player, state.Slot, flag);
				TransferLoadedWarden(state, player, returnPosition2);
			}
			return true;
		}
		Vector3 returnPosition3 = GetReturnPosition(player, state.Slot, flag);
		val.SetPosition(returnPosition3);
		val.Set(ZDOVars.s_follow, string.Empty);
		ZDOMan.instance.ForceSendZDO(val.m_uid);
		if (!state.ReportedPersistentSearch)
		{
			Logger.LogInfo((object)("Saved " + GetDisplayName(state.Slot) + " found. Calling them back..."));
			state.ReportedPersistentSearch = true;
		}
		state.SpawnDelay = 0.5f;
		return true;
	}

	private static bool DoesZdoBelongToSlot(ZDO zdo, WardenState state, long playerId)
	{
		if (zdo == null || !zdo.IsValid())
		{
			return false;
		}
		if (!zdo.GetBool("SoulboundCompanion_IsSoulbound", false))
		{
			return false;
		}
		if (zdo.GetLong("SoulboundCompanion_OwnerId", 0L) != playerId)
		{
			return false;
		}
		return zdo.GetInt("SoulboundCompanion_Slot", -1) == state.Slot;
	}

	private static void StampWardenIdentity(ZDO zdo, WardenState state, Player player)
	{
		if (zdo != null)
		{
			zdo.Set("SoulboundCompanion_IsSoulbound", true);
			zdo.Set("SoulboundCompanion_Slot", state.Slot);
			zdo.Set("SoulboundCompanion_Role", (int)state.Role);
			if ((Object)(object)player != (Object)null)
			{
				zdo.Set("SoulboundCompanion_OwnerId", player.GetPlayerID());
				zdo.Set("SoulboundCompanion_OwnerName", player.GetPlayerName());
			}
			zdo.Set(ZDOVars.s_follow, string.Empty);
			zdo.Set(ZDOVars.s_tamed, true);
			zdo.Persistent = true;
		}
	}

	private static List<ZDO> GetAllZDOsWithPrefab(string prefabName)
	{
		List<ZDO> list = new List<ZDO>();
		if (ZDOMan.instance == null)
		{
			return list;
		}
		List<ZDO> list2 = new List<ZDO>();
		int num = 0;
		bool flag = false;
		while (!flag)
		{
			list2.Clear();
			flag = ZDOMan.instance.GetAllZDOsWithPrefabIterative(prefabName, list2, ref num);
			foreach (ZDO item in list2)
			{
				if (item != null)
				{
					list.Add(item);
				}
			}
		}
		return list;
	}

	private void SpawnNewWarden(WardenState state, Player player)
	{
		//IL_008b: 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_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		string prefabNameForSlot = GetPrefabNameForSlot(state.Slot);
		GameObject prefab = PrefabManager.Instance.GetPrefab(prefabNameForSlot);
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogError((object)("Could not find prefab '" + prefabNameForSlot + "' for " + GetDisplayName(state.Slot) + "."));
			state.SpawnDel