Decompiled source of TurretsGoBrrrrrr v0.2.2

ValheimTurretMod.dll

Decompiled a week ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using JetBrains.Annotations;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ValheimTurretMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ValheimTurretMod")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4536e4c9-96dd-4a09-83fe-b7919bb1b2dc")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Mods.Turrets
{
	[BepInPlugin("de.davidfvp.vhmods.turrets", "TurretsGoBrrrrrr", "0.2.2")]
	public class TurretMod : BaseUnityPlugin
	{
		public static ConfigEntry<float> DamageModifier;

		public static ConfigEntry<float> HorizontalAngle;

		public static ConfigEntry<float> VerticalAngle;

		public static ConfigEntry<float> ViewDistance;

		public static ConfigEntry<float> AttackCooldown;

		public static ConfigEntry<float> AttackWarmup;

		public static ConfigEntry<bool> AttackBosses;

		public const string PluginId = "de.davidfvp.vhmods.turrets";

		public const string DisplayName = "TurretsGoBrrrrrr";

		public const string Version = "0.2.2";

		private Harmony _harmony;

		private static List<string> _keysBossesDefeated = new List<string> { "defeated_eikthyr", "defeated_elder", "defeated_bonemass", "defeated_dragon", "defeated_goblinking" };

		[UsedImplicitly]
		private void Awake()
		{
			DamageModifier = ((BaseUnityPlugin)this).Config.Bind<float>("Damage", "DamageModifier", 0.5f, "Multiplier for turret damage output.");
			AttackCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("Damage", "AttackCooldown", 0.5f, "Cooldown time in seconds between turret attacks.");
			AttackWarmup = ((BaseUnityPlugin)this).Config.Bind<float>("Damage", "AttackWarmup", 0.5f, "Time in seconds before the turret fires its first shot after locking onto a target.");
			HorizontalAngle = ((BaseUnityPlugin)this).Config.Bind<float>("Targeting", "HorizontalAngle", 80f, "Horizontal field of view (in degrees) for scanning targets.");
			VerticalAngle = ((BaseUnityPlugin)this).Config.Bind<float>("Targeting", "VerticalAngle", 35f, "Vertical field of view (in degrees) for scanning targets.");
			ViewDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Targeting", "ViewDistance", 40f, "Maximum detection range (in meters) for targets.");
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "de.davidfvp.vhmods.turrets");
		}

		[UsedImplicitly]
		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		public static int GetBossDefeatCount()
		{
			return _keysBossesDefeated.Count((string key) => ZoneSystem.instance.GetGlobalKey(key));
		}
	}
}
namespace Mods.Turrets.Patches
{
	[HarmonyPatch(typeof(Projectile))]
	public static class Projectile_Patch
	{
		[HarmonyPatch("Setup")]
		[HarmonyPrefix]
		private static bool Setup(Character owner, Vector3 velocity, float hitNoise, HitData hitData, ItemData item, ItemData ammo)
		{
			if ("$item_turretbolt".Equals(ammo?.m_shared?.m_name))
			{
				float num = (0.1f + (float)TurretMod.GetBossDefeatCount() * 0.2f) * TurretMod.DamageModifier.Value;
				((DamageTypes)(ref hitData.m_damage)).Modify(num);
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Turret))]
	public static class Turret_Patch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void ChangeTargetBehaviour(Turret __instance, int ___m_maxAmmo, ZNetView ___m_nview)
		{
			if (!((Object)(object)__instance == (Object)null))
			{
				__instance.m_targetPlayers = false;
				__instance.m_targetTamed = false;
				__instance.m_targetEnemies = true;
				__instance.m_horizontalAngle = TurretMod.HorizontalAngle.Value;
				__instance.m_verticalAngle = TurretMod.VerticalAngle.Value;
				__instance.m_viewDistance = TurretMod.ViewDistance.Value;
				__instance.m_attackCooldown = TurretMod.AttackCooldown.Value;
				__instance.m_attackWarmup = TurretMod.AttackWarmup.Value;
				__instance.m_maxAmmo = 0;
				__instance.m_defaultAmmo = ZNetScene.instance.GetPrefab("TurretBolt").GetComponent<ItemDrop>();
			}
		}
	}
	[HarmonyPatch(typeof(ZNetScene))]
	public static class ZNetScene_Patch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void ChangePieceCosts(ZNetScene __instance)
		{
			Debug.Log((object)$"GetPrefabNames: {__instance.GetPrefabNames().Count}");
			List<string> prefabNames = __instance.GetPrefabNames();
			if (prefabNames != null)
			{
				ChangeTurretCosts(__instance, prefabNames);
			}
		}

		private static void ChangeTurretCosts(ZNetScene scene, List<string> prefabs)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			GameObject prefab = scene.GetPrefab("piece_turret");
			if (!((Object)(object)prefab == (Object)null))
			{
				Piece component = prefab.GetComponent<Piece>();
				if (!((Object)(object)component == (Object)null))
				{
					component.m_resources = (Requirement[])(object)new Requirement[1]
					{
						new Requirement
						{
							m_resItem = scene.GetPrefab("Wood").GetComponent<ItemDrop>(),
							m_amount = 10
						}
					};
				}
			}
		}
	}
}