Decompiled source of Scary Coil Head v1.0.1

BepInEx/plugins/ScarySpringMan.dll

Decompiled 7 months ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LethalConfig;
using LethalConfig.ConfigItems;
using LethalConfig.ConfigItems.Options;
using ScarySpringMan.Patches;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
using UnityEngine.AI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ScarySpringMan")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ScarySpringMan")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a2ea0121-cbf4-4689-8d62-85c2b28711cd")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ScarySpringMan
{
	internal class ConfigManager
	{
		public static void setupLethalCongfig()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_0022: Expected O, but got Unknown
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			ConfigEntry<int> configMoveProb = ScarySpringManBase.configMoveProb;
			IntSliderOptions val = new IntSliderOptions();
			((BaseRangeOptions<int>)val).Min = 10;
			((BaseRangeOptions<int>)val).Max = 120;
			IntSliderConfigItem val2 = new IntSliderConfigItem(configMoveProb, val);
			BoolCheckBoxConfigItem val3 = new BoolCheckBoxConfigItem(ScarySpringManBase.configAttack, false);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val3);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val3);
		}
	}
	[BepInPlugin("Goobius.ScaryCoilHead", "Scary Coil Head", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class ScarySpringManBase : BaseUnityPlugin
	{
		public static ConfigEntry<bool> configAttack;

		public static ConfigEntry<int> configMoveProb;

		private const string modGUID = "Goobius.ScaryCoilHead";

		private const string modName = "Scary Coil Head";

		private const string modVersion = "1.0.1";

		private readonly Harmony harmony = new Harmony("Goobius.ScaryCoilHead");

		internal static ManualLogSource mls = Logger.CreateLogSource("Goobius.ScaryCoilHead");

		private void Awake()
		{
			configAttack = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Can Attack", false, "If enabled Coil Head can attack when players are looking");
			configMoveProb = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Movement Probability", 60, "Changed amount of seconds player needs to look at Coil Head before it moves. Note: probability of movement is calculated as 100% chance Coil Head will move in a given time, i.e. can move earlier or later than defined time");
			if (Chainloader.PluginInfos.ContainsKey("ainavt.lc.lethalconfig"))
			{
				ConfigManager.setupLethalCongfig();
			}
			mls.LogInfo((object)"Scary Spring Man is ALIVE :)");
			harmony.PatchAll(typeof(ScarySpringManBase));
			harmony.PatchAll(typeof(SpringManAIPatch));
		}
	}
}
namespace ScarySpringMan.Patches
{
	[HarmonyPatch(typeof(SpringManAI))]
	internal class SpringManAIPatch : EnemyAI
	{
		private static Random rnd = new Random();

		public static bool multiplePeps = false;

		public static bool kill = false;

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void patchUpdate(SpringManAI __instance)
		{
			if (((NetworkBehaviour)__instance).IsOwner && ShouldStartMoving(__instance))
			{
				((MonoBehaviour)__instance).StartCoroutine(MoveTowardsPlayer(__instance));
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		public static void placeNetworkTransfom(SpringManAI __instance)
		{
			if (!Object.op_Implicit((Object)(object)((Component)__instance).gameObject.GetComponent<NetworkTransform>()))
			{
				((Component)__instance).gameObject.AddComponent<NetworkTransform>();
			}
		}

		private static bool ShouldStartMoving(SpringManAI __instance)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: 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)
			bool flag = false;
			int num = 0;
			multiplePeps = false;
			for (int i = 0; i < StartOfRound.Instance.allPlayerScripts.Length; i++)
			{
				if (((EnemyAI)__instance).PlayerIsTargetable(StartOfRound.Instance.allPlayerScripts[i], false, false) && StartOfRound.Instance.allPlayerScripts[i].HasLineOfSightToPosition(((Component)__instance).transform.position + Vector3.up * 1.6f, 68f, 60, -1f) && Vector3.Distance(((Component)StartOfRound.Instance.allPlayerScripts[i].gameplayCamera).transform.position, ((EnemyAI)__instance).eye.position) > 0.3f)
				{
					flag = true;
					num++;
				}
			}
			if (flag)
			{
				int maxValue = ScarySpringManBase.configMoveProb.Value * 60;
				int num2 = rnd.Next(1, maxValue);
				if (num2 == 420)
				{
					if (ScarySpringManBase.configAttack.Value)
					{
						if (num == 2)
						{
							int num3 = rnd.Next(1, 100);
							if (num3 == 69)
							{
								multiplePeps = true;
							}
						}
						else if (num == 3)
						{
							int num3 = rnd.Next(1, 10);
							if (num3 == 8)
							{
								multiplePeps = true;
							}
						}
						else if (num >= 4)
						{
							int num4 = num - 4;
							int num5 = 10 - num4 * 2;
							int num3 = ((num5 <= 1) ? 1 : rnd.Next(1, num5));
							if (num3 == 1)
							{
								multiplePeps = true;
							}
						}
					}
					return true;
				}
			}
			return false;
		}

		private static IEnumerator MoveTowardsPlayer(SpringManAI __instance)
		{
			if ((!((Behaviour)((EnemyAI)__instance).agent).enabled || !((EnemyAI)__instance).agent.isOnNavMesh) && !((EnemyAI)__instance).agent.Warp(((Component)__instance).transform.position))
			{
				ScarySpringManBase.mls.LogInfo((object)"Cannot place on NavMesh");
				yield break;
			}
			float currentSpeed = 0f;
			float targetSpeed = 14.5f;
			float acceleration = 2.5f;
			((EnemyAI)__instance).agent.angularSpeed = 270f;
			((EnemyAI)__instance).agent.stoppingDistance = 0.5f;
			((EnemyAI)__instance).agent.updatePosition = true;
			((EnemyAI)__instance).agent.updateRotation = true;
			float distanceScalar = ((!multiplePeps) ? 0.5f : 1.1f);
			Vector3 PlayerPosition = ((Component)GameNetworkManager.Instance.localPlayerController).transform.position;
			Vector3 directionToPlayer = PlayerPosition - ((Component)__instance).transform.position;
			Vector3 targetPosition = ((Component)__instance).transform.position + directionToPlayer * distanceScalar;
			float distanceToTarget = Vector3.Distance(((Component)__instance).transform.position, targetPosition);
			float maxDistance = 10f;
			NavMeshHit hit = default(NavMeshHit);
			if (NavMesh.SamplePosition(targetPosition, ref hit, maxDistance, -1))
			{
				targetPosition = ((NavMeshHit)(ref hit)).position;
			}
			else
			{
				ScarySpringManBase.mls.LogInfo((object)"Failed to find a point on the NavMesh near the target position.");
			}
			if (NavMesh.SamplePosition(targetPosition, ref hit, maxDistance, -1))
			{
				targetPosition = ((NavMeshHit)(ref hit)).position;
			}
			((EnemyAI)__instance).agent.SetDestination(targetPosition);
			yield return (object)new WaitWhile((Func<bool>)(() => ((EnemyAI)__instance).agent.pathPending));
			((EnemyAI)__instance).agent.isStopped = false;
			while (distanceToTarget > ((EnemyAI)__instance).agent.stoppingDistance)
			{
				bool playerLooking = false;
				for (int i = 0; i < StartOfRound.Instance.allPlayerScripts.Length; i++)
				{
					if (((EnemyAI)__instance).PlayerIsTargetable(StartOfRound.Instance.allPlayerScripts[i], false, false) && StartOfRound.Instance.allPlayerScripts[i].HasLineOfSightToPosition(((Component)__instance).transform.position + Vector3.up * 1.6f, 68f, 60, -1f) && Vector3.Distance(((Component)StartOfRound.Instance.allPlayerScripts[i].gameplayCamera).transform.position, ((EnemyAI)__instance).eye.position) > 0.3f)
					{
						playerLooking = true;
						break;
					}
				}
				if (!playerLooking || !((NetworkBehaviour)__instance).IsOwner)
				{
					yield break;
				}
				distanceToTarget = Vector3.Distance(((Component)__instance).transform.position, targetPosition);
				currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, acceleration * Time.deltaTime);
				((EnemyAI)__instance).agent.speed = currentSpeed;
				__instance.SetAnimationGoServerRpc();
				if (distanceToTarget <= ((EnemyAI)__instance).agent.stoppingDistance + 0.5f)
				{
					break;
				}
				ulong ownerID = ((NetworkBehaviour)__instance).OwnerClientId;
				if (!((EnemyAI)__instance).PlayerIsTargetable(StartOfRound.Instance.allPlayerScripts[ownerID], false, false))
				{
					break;
				}
				yield return null;
			}
			((EnemyAI)__instance).agent.speed = 0f;
			__instance.SetAnimationStopServerRpc();
			((EnemyAI)__instance).agent.ResetPath();
		}
	}
}