Decompiled source of Feta Footing Terrain Ping v1.0.0

TerrainPing.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

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

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
[BepInPlugin("dairymods.terrainping", "Terrain Ping", "1.0.0")]
public class TerrainPingPlugin : BaseUnityPlugin
{
	private struct Spot
	{
		public Vector3 p;

		public bool reachable;

		public bool visible;
	}

	private ConfigEntry<Key> key;

	private ConfigEntry<float> radius;

	private ConfigEntry<float> step;

	private ConfigEntry<float> seconds;

	private ConfigEntry<float> standAngle;

	private ConfigEntry<float> margin;

	private readonly List<Spot> spots = new List<Spot>();

	private float until;

	private float climbedMetres;

	private float climbedStamina;

	private float lastStamina;

	private Vector3 lastPos;

	private bool wasClimbing;

	private void Awake()
	{
		key = ((BaseUnityPlugin)this).Config.Bind<Key>("General", "Ping Key", (Key)21, "Press to mark standable ground around you.");
		radius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Radius", 15f, "Metres.");
		step = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Grid Step", 1.5f, "Metres between samples. Smaller = more dots, more work per press.");
		seconds = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Seconds", 6f, "How long the dots stay.");
		standAngle = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Stand Angle", 50f, "Ground flatter than this many degrees counts as standable.");
		margin = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Stamina Margin", 0.85f, "Share of your stamina the estimate may spend. Lower = more cautious, more red.");
	}

	private static float MetresPerBar(Character me)
	{
		CharacterClimbing climbing = me.refs.climbing;
		float num = climbing.maxStaminaUsage * Ascents.climbStaminaMultiplier;
		if (!(num > 0f))
		{
			return float.PositiveInfinity;
		}
		return climbing.climbSpeed * climbing.climbSpeedMod / num;
	}

	private static float AngleCost(float grade)
	{
		return Mathf.Lerp(0.2f, 1f, Mathf.InverseLerp(40f, 60f, grade));
	}

	private void Update()
	{
		//IL_0027: 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_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: 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_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		Character localCharacter = Character.localCharacter;
		if (!Object.op_Implicit((Object)(object)localCharacter))
		{
			spots.Clear();
			return;
		}
		Learn(localCharacter);
		if ((int)key.Value != 0 && Keyboard.current != null && ((ButtonControl)Keyboard.current[key.Value]).wasPressedThisFrame)
		{
			Ping(localCharacter);
		}
		if (Time.time >= until)
		{
			spots.Clear();
			return;
		}
		Camera cam = Draw.Cam;
		if (Object.op_Implicit((Object)(object)cam))
		{
			Vector3 position = ((Component)cam).transform.position;
			for (int i = 0; i < spots.Count; i++)
			{
				Spot value = spots[i];
				value.visible = !Physics.Linecast(position, value.p + Vector3.up * 0.2f, LayerMask.op_Implicit(HelperFunctions.terrainMapMask), (QueryTriggerInteraction)1);
				spots[i] = value;
			}
		}
	}

	private void Learn(Character me)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: 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_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		float totalStamina = me.GetTotalStamina();
		Vector3 center = me.Center;
		if (me.data.isClimbing && wasClimbing)
		{
			float num = lastStamina - totalStamina;
			float num2 = Vector3.Distance(center, lastPos);
			if (num > 0f && num2 < 1f)
			{
				climbedStamina += num;
				climbedMetres += num2;
			}
		}
		wasClimbing = me.data.isClimbing;
		lastStamina = totalStamina;
		lastPos = center;
	}

	private void Ping(Character me)
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: 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_0084: 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_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0206: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: 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)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: 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_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_011a: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: 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_0151: 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_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
		spots.Clear();
		until = Time.time + seconds.Value;
		LayerMask terrainMapMask = HelperFunctions.terrainMapMask;
		Vector3 center = me.Center;
		float value = radius.Value;
		float num = MetresPerBar(me);
		float num2 = me.GetTotalStamina() * margin.Value;
		int num3 = 0;
		RaycastHit val2 = default(RaycastHit);
		for (float num4 = 0f - value; num4 <= value; num4 += step.Value)
		{
			for (float num5 = 0f - value; num5 <= value; num5 += step.Value)
			{
				if (num4 * num4 + num5 * num5 > value * value)
				{
					continue;
				}
				Vector3 val = center + new Vector3(num4, value, num5);
				float num6 = center.y - value;
				for (int i = 0; i < 6; i++)
				{
					if (!(val.y > num6))
					{
						break;
					}
					if (!Physics.Raycast(val, Vector3.down, ref val2, val.y - num6, LayerMask.op_Implicit(terrainMapMask), (QueryTriggerInteraction)1))
					{
						break;
					}
					val = ((RaycastHit)(ref val2)).point + Vector3.down * 0.3f;
					if (!(Vector3.Angle(((RaycastHit)(ref val2)).normal, Vector3.up) >= standAngle.Value) && !Physics.Raycast(((RaycastHit)(ref val2)).point + Vector3.up * 0.1f, Vector3.up, 1.6f, LayerMask.op_Implicit(terrainMapMask), (QueryTriggerInteraction)1) && !(Vector3.Distance(center, ((RaycastHit)(ref val2)).point) > value))
					{
						Vector2 val3 = new Vector2(num4, num5);
						float magnitude = ((Vector2)(ref val3)).magnitude;
						float num7 = ((RaycastHit)(ref val2)).point.y - (center.y - 1f);
						float num8 = Mathf.Atan2(num7, magnitude) * 57.29578f;
						bool flag = num7 <= 0.5f || num8 < 40f || Mathf.Sqrt(num7 * num7 + magnitude * magnitude) * AngleCost(num8) / num <= num2;
						if (!flag)
						{
							num3++;
						}
						spots.Add(new Spot
						{
							p = ((RaycastHit)(ref val2)).point,
							reachable = flag
						});
					}
				}
			}
		}
		string text = ((climbedStamina > 0.2f) ? $"{climbedMetres / climbedStamina:0.0}" : "n/a");
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"Ping: {spots.Count} spots ({num3} red), stamina {me.GetTotalStamina():0.00}, formula {num:0.0} m per bar, measured {text}, ascent x{Ascents.climbStaminaMultiplier:0.00}");
	}

	private void OnGUI()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Invalid comparison between Unknown and I4
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		if ((int)Event.current.type != 7 || spots.Count == 0)
		{
			return;
		}
		Camera cam = Draw.Cam;
		if (!Object.op_Implicit((Object)(object)cam))
		{
			return;
		}
		float num = Mathf.Clamp01(until - Time.time);
		Vector3 position = ((Component)cam).transform.position;
		foreach (Spot spot in spots)
		{
			if (spot.visible && Draw.ToGui(cam, spot.p, out var gui))
			{
				Color c = (spot.reachable ? new Color(0.2f, 1f, 0.3f, num) : new Color(1f, 0.25f, 0.2f, num));
				Draw.Dot(gui, c, Mathf.Clamp(100f / Vector3.Distance(position, spot.p), 4f, 12f));
			}
		}
	}
}
internal static class Draw
{
	private static GUIStyle style;

	public static Camera Cam
	{
		get
		{
			MainCamera instance = MainCamera.instance;
			Camera val = (Object.op_Implicit((Object)(object)instance) ? ((Component)instance).GetComponent<Camera>() : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				return Camera.main;
			}
			return val;
		}
	}

	public static bool ToGui(Camera cam, Vector3 world, out Vector2 gui)
	{
		//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_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: 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)
		Vector3 val = cam.WorldToViewportPoint(world);
		gui = new Vector2(val.x * (float)Screen.width, (1f - val.y) * (float)Screen.height);
		return val.z > 0f;
	}

	public static void Line(Vector2 a, Vector2 b, Color c, float width)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: 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)
		Matrix4x4 matrix = GUI.matrix;
		Vector2 val = b - a;
		GUI.color = c;
		GUIUtility.RotateAroundPivot(Mathf.Atan2(val.y, val.x) * 57.29578f, a);
		GUI.DrawTexture(new Rect(a.x, a.y - width / 2f, ((Vector2)(ref val)).magnitude, width), (Texture)(object)Texture2D.whiteTexture);
		GUI.matrix = matrix;
		GUI.color = Color.white;
	}

	public static void Dot(Vector2 p, Color c, float size)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		GUI.color = c;
		GUI.DrawTexture(new Rect(p.x - size / 2f, p.y - size / 2f, size, size), (Texture)(object)Texture2D.whiteTexture);
		GUI.color = Color.white;
	}

	public static void Text(Rect r, string s, Color c, int size = 18, TextAnchor anchor = (TextAnchor)0)
	{
		//IL_0039: 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_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		if (style == null)
		{
			style = new GUIStyle(GUI.skin.label)
			{
				fontStyle = (FontStyle)1,
				wordWrap = false
			};
		}
		style.fontSize = size;
		style.alignment = anchor;
		style.normal.textColor = new Color(0f, 0f, 0f, c.a * 0.8f);
		GUI.Label(new Rect(((Rect)(ref r)).x + 2f, ((Rect)(ref r)).y + 2f, ((Rect)(ref r)).width, ((Rect)(ref r)).height), s, style);
		style.normal.textColor = c;
		GUI.Label(r, s, style);
	}
}