From df1bdc7b8545b8626937418cb074a99ed9a91b99 Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Wed, 15 Jun 2022 11:10:28 -0400 Subject: [PATCH] Nomai Coordinates --- .../External/Configs/StarSystemConfig.cs | 14 ++++ NewHorizons/Main.cs | 22 +++++- NewHorizons/Patches/NomaiCoordinatePatches.cs | 74 +++++++++++++++++++ NewHorizons/Utility/NewHorizonExtensions.cs | 29 +++++++- 4 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 NewHorizons/Patches/NomaiCoordinatePatches.cs diff --git a/NewHorizons/External/Configs/StarSystemConfig.cs b/NewHorizons/External/Configs/StarSystemConfig.cs index 5d5cdc75..d9cbba36 100644 --- a/NewHorizons/External/Configs/StarSystemConfig.cs +++ b/NewHorizons/External/Configs/StarSystemConfig.cs @@ -1,4 +1,6 @@ using System.ComponentModel; +using System.Linq; +using NewHorizons.Utility; using Newtonsoft.Json; namespace NewHorizons.External.Configs @@ -56,6 +58,11 @@ namespace NewHorizons.External.Configs /// public string travelAudioFilePath; + /// + /// Coordinates that the vessel can use to warp to your solar system. + /// + public NomaiCoordinates coords; + public class NomaiCoordinates { public int[] x; @@ -81,5 +88,12 @@ namespace NewHorizons.External.Configs /// public string path; } + + public void FixCoordinates() + { + coords.x = coords.x.Distinct().ToArray(); + coords.y = coords.y.Distinct().ToArray(); + coords.z = coords.z.Distinct().ToArray(); + } } } \ No newline at end of file diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 35bf01b9..35ca6dce 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -108,7 +108,25 @@ namespace NewHorizons { Config = { - destroyStockPlanets = false + destroyStockPlanets = false, + coords = new StarSystemConfig.NomaiCoordinates + { + x = new int[5]{ 0,3,2,1,5 }, + y = new int[5]{ 4,5,3,2,1 }, + z = new int[5]{ 4,1,2,5,0 } + } + } + }; + SystemDict["EyeOfTheUniverse"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(), Instance) + { + Config = + { + coords = new StarSystemConfig.NomaiCoordinates + { + x = new int[3]{ 1,5,4 }, + y = new int[4]{ 3,0,1,4 }, + z = new int[6]{ 1,2,3,0,5,4 } + } } }; @@ -326,6 +344,7 @@ namespace NewHorizons var relativePath = file.Replace(folder, ""); var starSystemConfig = mod.ModHelper.Storage.Load(relativePath); + starSystemConfig.FixCoordinates(); if (starSystemConfig.startHere) { @@ -405,6 +424,7 @@ namespace NewHorizons { // Since we didn't load it earlier there shouldn't be a star system config var starSystemConfig = mod.ModHelper.Storage.Load($"systems/{config.starSystem}.json"); + starSystemConfig.FixCoordinates(); if (starSystemConfig == null) starSystemConfig = new StarSystemConfig(); else Logger.LogWarning($"Loaded system config for {config.starSystem}. Why wasn't this loaded earlier?"); diff --git a/NewHorizons/Patches/NomaiCoordinatePatches.cs b/NewHorizons/Patches/NomaiCoordinatePatches.cs new file mode 100644 index 00000000..1814b9a1 --- /dev/null +++ b/NewHorizons/Patches/NomaiCoordinatePatches.cs @@ -0,0 +1,74 @@ +using HarmonyLib; +using NewHorizons.Utility; +using UnityEngine; +namespace NewHorizons.Patches +{ + [HarmonyPatch] + public static class NomaiCoordinatePatches + { + [HarmonyPrefix] + [HarmonyPatch(typeof(NomaiCoordinateInterface), nameof(NomaiCoordinateInterface.SetPillarRaised), new System.Type[] { typeof(bool) })] + public static bool NomaiCoordinateInterface_SetPillarRaised(NomaiCoordinateInterface __instance, bool raised) + { + if (raised) + return !(!__instance._powered || (__instance.CheckEyeCoordinates() && Main.Instance.CurrentStarSystem != "EyeOfTheUniverse") || (__instance.CheckAllCoordinates(out string targetSystem) && Main.Instance.CurrentStarSystem != targetSystem)); + return true; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(VesselWarpController), nameof(VesselWarpController.WarpVessel))] + public static bool VesselWarpController_WarpVessel(VesselWarpController __instance, bool debugWarp) + { + if (!Main.Instance.IsWarpingFromVessel && TimeLoop.GetLoopCount() < 2) + Achievements.Earn(Achievements.Type.BEGINNERS_LUCK); + VesselWarpController.s_playerWarpLocation = new RelativeLocationData(Locator.GetPlayerBody(), __instance.transform); + VesselWarpController.s_relativeLocationSaved = !debugWarp; + if (!Main.Instance.IsWarpingFromVessel) + PlayerData.SaveWarpedToTheEye(TimeLoop.GetSecondsRemaining()); + LoadManager.EnableAsyncLoadTransition(); + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(VesselWarpController), nameof(VesselWarpController.CheckSystemActivation))] + public static void VesselWarpController_CheckSystemActivation(VesselWarpController __instance) + { + if (Locator.GetEyeStateManager() == null && Main.Instance.CurrentStarSystem != "EyeOfTheUniverse") + { + if (!__instance._sourceWarpPlatform.IsBlackHoleOpen() && __instance._hasPower && __instance._warpPlatformPowerSlot.IsActivated() && __instance._targetWarpPlatform != null) + __instance._sourceWarpPlatform.OpenBlackHole(__instance._targetWarpPlatform, true); + else if (__instance._sourceWarpPlatform.IsBlackHoleOpen() && (!__instance._hasPower || !__instance._warpPlatformPowerSlot.IsActivated())) + __instance._sourceWarpPlatform.CloseBlackHole(); + } + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(VesselWarpController), nameof(VesselWarpController.OnSlotActivated))] + public static bool VesselWarpController_OnSlotActivated(VesselWarpController __instance, NomaiInterfaceSlot slot) + { + bool canWarpToEye = __instance._coordinateInterface.CheckEyeCoordinates(); + bool canWarpToStarSystem = __instance._coordinateInterface.CheckAllCoordinates(out string targetSystem); + if (slot == __instance._warpVesselSlot && __instance._hasPower && (canWarpToEye || (canWarpToStarSystem && targetSystem != Main.Instance.CurrentStarSystem)) && __instance._blackHole.GetState() == SingularityController.State.Collapsed && LoadManager.GetCurrentScene() != OWScene.EyeOfTheUniverse) + { + __instance._blackHole.Create(); + RumbleManager.StartVesselWarp(); + __instance._openingBlackHole = true; + __instance.enabled = true; + Locator.GetPauseCommandListener().AddPauseCommandLock(); + if (canWarpToEye || (canWarpToStarSystem && targetSystem == "EyeOfTheUniverse")) + LoadManager.LoadSceneAsync(OWScene.EyeOfTheUniverse, false, LoadManager.FadeType.ToWhite); + else if (canWarpToStarSystem) + Main.Instance.ChangeCurrentStarSystem(targetSystem, false, true); + __instance._blackHoleOneShot.PlayOneShot(AudioType.VesselSingularityCreate); + GlobalMessenger.FireEvent("StartVesselWarp"); + } + else + { + if (slot == __instance._coordinatePowerSlot) + __instance._coordinateInterface.SetPillarRaised(true, true); + __instance.CheckSystemActivation(); + } + return false; + } + } +} diff --git a/NewHorizons/Utility/NewHorizonExtensions.cs b/NewHorizons/Utility/NewHorizonExtensions.cs index 3f0ecc83..9c04d33d 100644 --- a/NewHorizons/Utility/NewHorizonExtensions.cs +++ b/NewHorizons/Utility/NewHorizonExtensions.cs @@ -1,9 +1,11 @@ -using System; +using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using UnityEngine; +using NomaiCoordinates = NewHorizons.External.Configs.StarSystemConfig.NomaiCoordinates; namespace NewHorizons.Utility { public static class NewHorizonsExtensions @@ -122,5 +124,30 @@ namespace NewHorizons.Utility { return transform.rotation * localRotation; } + + public static bool CheckAllCoordinates(this NomaiCoordinateInterface nomaiCoordinateInterface) => Main.SystemDict.Where(system => system.Value.Config.coords != null).Select(system => new KeyValuePair(system.Key, system.Value.Config.coords)).Any(system => nomaiCoordinateInterface.CheckCoordinates(system.Key, system.Value)); + + public static bool CheckAllCoordinates(this NomaiCoordinateInterface nomaiCoordinateInterface, out string selectedSystem) + { + foreach (KeyValuePair cbs in Main.SystemDict.Where(system => system.Value.Config.coords != null).Select(system => new KeyValuePair(system.Key, system.Value.Config.coords))) + { + if (CheckCoordinates(nomaiCoordinateInterface, cbs.Key, cbs.Value)) + { + selectedSystem = cbs.Key; + return true; + } + } + selectedSystem = null; + return false; + } + + public static bool CheckCoordinates(this NomaiCoordinateInterface nomaiCoordinateInterface, string system, NomaiCoordinates coordinates) + { + bool xCorrect = nomaiCoordinateInterface._nodeControllers[0].CheckCoordinate(coordinates.x); + bool yCorrect = nomaiCoordinateInterface._nodeControllers[1].CheckCoordinate(coordinates.y); + bool zCorrect = nomaiCoordinateInterface._nodeControllers[2].CheckCoordinate(coordinates.z); + Utility.Logger.Log($"Coordinate Check for {system}: {xCorrect}, {yCorrect}, {zCorrect} [{string.Join("-", coordinates.x)}, {string.Join("-", coordinates.y)}, {string.Join("-", coordinates.z)}]"); + return xCorrect && yCorrect && zCorrect; + } } }