Decompiled source of BetterPathfinding v1.4.4

plugins/BetterPathfinding.dll

Decompiled 4 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.0", FrameworkDisplayName = ".NET Framework 4")]
[assembly: AssemblyCompany("BetterPathfinding")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Improves Icarus' built-in pathfinding system")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BetterPathfinding")]
[assembly: AssemblyTitle("BetterPathfinding")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BetterPathfinding
{
	[BepInPlugin("BetterPathfinding", "BetterPathfinding", "1.0.0")]
	public class BetterPathfinding : BaseUnityPlugin
	{
		public static ConfigEntry<int> flightActivationDistance;

		public static ConfigEntry<bool> shouldAlwaysLand;

		public static ConfigEntry<bool> allowPathingInPlanetView;

		public static ConfigEntry<bool> autoRestockReactor;

		internal static ManualLogSource Log;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			harmony = new Harmony("BetterPathfinding.Patch");
			harmony.PatchAll(typeof(BetterPathfindingPatches));
			flightActivationDistance = ((BaseUnityPlugin)this).Config.Bind<int>("General", "FlightActivationDistance", 100, new ConfigDescription("Min distance to fly to the target instead of walk", (AcceptableValueBase)(object)new AcceptableValueRange<int>(10, int.MaxValue), new object[0]));
			shouldAlwaysLand = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShouldAlwaysLand", false, "Determines if Icarus should auto-land if pathfinding started in flight");
			allowPathingInPlanetView = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowPathingInPlanetView", true, "Determines if you are able to right-click on a location in planet view to move there");
			autoRestockReactor = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AutoRestockReactor", false, "Determines if Icarus should auto-pathfind to and mine coal to restock the reactor when on low power");
		}
	}
	public class BetterPathfindingPatches
	{
		private static int unstuckTimer = 0;

		private static bool isPathing = false;

		private static bool donePathing = false;

		private static bool isPathingByFlight = false;

		private static bool shouldLand = false;

		private static int landingTries = 0;

		private static int ignoreTimer = 0;

		private static RaycastHit hitInfo = default(RaycastHit);

		private static bool hit = false;

		private static Vector3 dragBeginMousePosition;

		private static int latestCoalVeinId = 0;

		[HarmonyPatch(typeof(PlayerMove_Walk), "GameTick")]
		[HarmonyPrefix]
		public static void GameTick_Prefix(PlayerMove_Walk __instance)
		{
			//IL_0041: 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_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_0217: Invalid comparison between Unknown and I4
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			Player player = ((PlayerAction)__instance).player;
			OrderNode currentOrder = player.currentOrder;
			isPathing = false;
			if (currentOrder != null && !currentOrder.targetReached)
			{
				isPathing = true;
				donePathing = false;
				if (__instance.isGrounded && Vector3.Distance(currentOrder.target, player.position) > (float)Mathf.Max(10, BetterPathfinding.flightActivationDistance.Value) && __instance.mecha.thrusterLevel > 0 && !__instance.mecha.EnergyIsLow())
				{
					__instance.SwitchToFly();
				}
				if (unstuckTimer != 0)
				{
					switch (unstuckTimer)
					{
					case 2:
						if (!isPathingByFlight && player.speed >= 2f)
						{
							shouldLand = true;
						}
						unstuckTimer = 0;
						break;
					case 1:
						TrySwitchToFlyOrJump(__instance);
						unstuckTimer = 30;
						break;
					default:
						unstuckTimer--;
						break;
					}
				}
				if (ignoreTimer == 0 && player.speed < 2f && Vector3.Distance(currentOrder.target, player.position) > 5f)
				{
					unstuckTimer = 1;
				}
				if (ignoreTimer != 0)
				{
					ignoreTimer--;
				}
			}
			if (!isPathing)
			{
				ignoreTimer = 35;
				unstuckTimer = 0;
				if (!donePathing && (isPathingByFlight || BetterPathfinding.shouldAlwaysLand.Value))
				{
					shouldLand = true;
				}
				isPathingByFlight = false;
				donePathing = true;
			}
			if (shouldLand)
			{
				landingTries++;
				((PlayerAction)__instance).controller.actionFly.targetAltitude = 1f;
				if (__instance.isGrounded || landingTries > 20 || (((PlayerAction)__instance).player != null && ((PlayerAction)__instance).player.planetData != null && (int)((PlayerAction)__instance).player.planetData.type == 5))
				{
					shouldLand = false;
					landingTries = 0;
				}
			}
		}

		private static void TrySwitchToFlyOrJump(PlayerMove_Walk actionWalk)
		{
			if (actionWalk.mecha.thrusterLevel >= 1)
			{
				if (actionWalk.isGrounded)
				{
					actionWalk.SwitchToFly();
				}
				else
				{
					((PlayerAction)actionWalk).player.controller.actionFly.targetAltitude = 49f;
				}
			}
			else if (actionWalk.jumpCoolTime == 0f)
			{
				actionWalk.jumpCoolTime = 0.3f;
				actionWalk.jumpedTime = 0f;
				actionWalk.jumpDelayTick = actionWalk.jumpDelay;
				actionWalk.jumpStartVertSpeed = actionWalk.mecha.jumpSpeed;
				actionWalk.jumpingVertSpeed = 0f;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerController), "GameTick")]
		private static void PlayerControllerGameTick_Postfix(PlayerController __instance)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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_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_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			if (!((ManualBehaviour)UIRoot.instance.uiGame.globemap).active)
			{
				return;
			}
			if (BetterPathfinding.allowPathingInPlanetView.Value && VFInput._rtsMove.onDown)
			{
				dragBeginMousePosition = Input.mousePosition;
				hit = Physics.Raycast(Camera.main.ScreenPointToRay(dragBeginMousePosition), ref hitInfo, 800f, 8720, (QueryTriggerInteraction)2);
			}
			if (VFInput._rtsMove.onUp)
			{
				Vector3 val = dragBeginMousePosition - Input.mousePosition;
				if ((double)((Vector3)(ref val)).sqrMagnitude < 800.0 && hit)
				{
					GameMain.data.mainPlayer.Order(OrderNode.MoveTo(((RaycastHit)(ref hitInfo)).point), InputValue.op_Implicit(VFInput._multiOrdering));
					RTSTargetGizmo.Create(((RaycastHit)(ref hitInfo)).point);
					hit = false;
				}
			}
		}

		[HarmonyPatch(typeof(PlayerAction_Mine), "GameTick")]
		[HarmonyPrefix]
		public static void PlayerAction_Mine_Prefix(PlayerAction_Mine __instance)
		{
			//IL_00ba: 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)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Invalid comparison between Unknown and I4
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: 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_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_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: 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)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Invalid comparison between Unknown and I4
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Invalid comparison between Unknown and I4
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: 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_01e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: 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_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Invalid comparison between Unknown and I4
			Player player = ((PlayerAction)__instance).player;
			OrderNode currentOrder = player.currentOrder;
			if (player.mecha.EnergyIsLow() && BetterPathfinding.autoRestockReactor.Value)
			{
				if (!player.mecha.reactorStorage.isEmpty)
				{
					return;
				}
				int num = 1006;
				int num2 = default(int);
				int num3 = player.package.TakeItem(num, 1000, ref num2);
				if (num3 > 0)
				{
					player.mecha.reactorStorage.AddItem(num, num3, 0, ref num2, false);
				}
				else
				{
					if (player.planetData == null || player.planetData.factory.veinPool == null)
					{
						return;
					}
					Vector3 position = player.position;
					VeinData[] veinPool = player.planetData.factory.veinPool;
					VeinData val = default(VeinData);
					((VeinData)(ref val)).SetNull();
					VeinData[] array = veinPool;
					Vector3 val3;
					foreach (VeinData val2 in array)
					{
						if ((int)val2.type != 6)
						{
							continue;
						}
						if (val.id != 0)
						{
							val3 = position - val2.pos;
							float magnitude = ((Vector3)(ref val3)).magnitude;
							val3 = position - val.pos;
							if (!(magnitude < ((Vector3)(ref val3)).magnitude))
							{
								continue;
							}
						}
						val = val2;
					}
					if (val.pos != Vector3.zero && (currentOrder == null || (int)currentOrder.objType != 2 || ((int)currentOrder.objType == 2 && (int)player.planetData.factory.GetVeinData(currentOrder.objId).type != 6)))
					{
						val3 = position - val.pos;
						if (((Vector3)(ref val3)).magnitude > 10f)
						{
							player.Order(OrderNode.MineTarget(val.pos, (EObjectType)2, val.id, val.pos), false);
							latestCoalVeinId = val.id;
						}
					}
				}
			}
			else if (currentOrder != null && currentOrder.objId != 0 && currentOrder.objId == latestCoalVeinId)
			{
				player.Order(OrderNode.Stop, false);
			}
		}
	}
}