From dae09dc8e5424af5b7601015188c27411f452f21 Mon Sep 17 00:00:00 2001 From: Ben C Date: Sun, 8 May 2022 10:56:33 -0400 Subject: [PATCH] Started on a better load order --- NewHorizons/Handlers/PlanetCreationHandler.cs | 56 +++----- NewHorizons/Handlers/PlanetGraphHandler.cs | 126 ++++++++++++++++++ NewHorizons/Patches/RaftPatches.cs | 2 +- 3 files changed, 145 insertions(+), 39 deletions(-) create mode 100644 NewHorizons/Handlers/PlanetGraphHandler.cs diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index a3c7556c..a5a11ee4 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -20,7 +20,6 @@ namespace NewHorizons.Handlers public static class PlanetCreationHandler { public static List NextPassBodies = new List(); - private static List ToLoad; private static Dictionary ExistingAOConfigs; public static void Init(List bodies) @@ -57,49 +56,30 @@ namespace NewHorizons.Handlers starLightGO.SetActive(true); - // Order by stars then planets then moons (not necessary but probably speeds things up, maybe) ALSO only include current star system - ToLoad = bodies - .OrderBy(b => - (b.Config.BuildPriority != -1 ? b.Config.BuildPriority : - (b.Config.FocalPoint != null ? 0 : - (b.Config.Star != null) ? 0 : - (b.Config.Orbit.IsMoon ? 2 : 1) - ))).ToList(); + var planetGraph = new PlanetGraphHandler(bodies.OrderBy(b => b.Config?.BuildPriority ?? 0)); - var passCount = 0; - while (ToLoad.Count != 0) + foreach (var node in planetGraph) { - Logger.Log($"Starting body loading pass #{++passCount}"); - var flagNoneLoadedThisPass = true; - foreach (var body in ToLoad) + LoadBody(node.body); + if (node is PlanetGraphHandler.FocalPointNode focal) { - if (LoadBody(body)) flagNoneLoadedThisPass = false; - } - if (flagNoneLoadedThisPass) - { - Logger.LogWarning("No objects were loaded this pass"); - // Try again but default to sun - foreach (var body in ToLoad) - { - if (LoadBody(body, true)) flagNoneLoadedThisPass = false; - } - } - if (flagNoneLoadedThisPass) - { - // Give up - Logger.Log($"Couldn't finish adding bodies."); - return; + LoadBody(focal.primary.body); + LoadBody(focal.secondary.body); } + } - ToLoad = NextPassBodies; + var nextPassLoad = NextPassBodies.Select(x => x).ToList(); + + Logger.Log("Loading Deferred Bodies"); + + while (NextPassBodies.Count != 0) + { + foreach (var body in nextPassLoad) + { + LoadBody(body, true); + } + nextPassLoad = NextPassBodies; NextPassBodies = new List(); - - // Infinite loop failsafe - if (passCount > 10) - { - Logger.Log("Something went wrong"); - break; - } } Logger.Log("Done loading bodies"); diff --git a/NewHorizons/Handlers/PlanetGraphHandler.cs b/NewHorizons/Handlers/PlanetGraphHandler.cs new file mode 100644 index 00000000..f8b2ddb9 --- /dev/null +++ b/NewHorizons/Handlers/PlanetGraphHandler.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using JetBrains.Annotations; +using NewHorizons.External.Configs; +using NewHorizons.Utility; + +namespace NewHorizons.Handlers +{ + public class PlanetGraphHandler : IEnumerable + { + public class PlanetNode + { + public NewHorizonsBody body; + public IEnumerable children; + } + + public class FocalPointNode : PlanetNode + { + public PlanetNode primary; + public PlanetNode secondary; + } + + private PlanetNode _rootNode; + + public PlanetGraphHandler(IEnumerable iBodies) + { + var bodies = iBodies.ToArray(); + var centers = bodies.Where(b => b.Config.Base.CenterOfSolarSystem).ToArray(); + if (centers.Length == 1) + { + _rootNode = ConstructGraph(centers[0], bodies); + } + else + { + if (centers.Length == 0 && Main.Instance.CurrentStarSystem == "SolarSystem") + { + var SunConfig = new Dictionary + { + {"name", "Sun"} + }; + _rootNode = ConstructGraph(new NewHorizonsBody(new PlanetConfig(SunConfig), Main.Instance), bodies); + } + else + { + Logger.LogError("There must be one and only one centerOfSolarSystem!"); + } + } + } + + private static bool DetermineIfChildOfFocal(NewHorizonsBody body, FocalPointNode node) + { + var name = body.Config.Name.ToLower(); + var primary = (body.Config.Orbit?.PrimaryBody ?? "").ToLower(); + var primaryName = node.primary.body.Config.Name.ToLower(); + var secondaryName = node.secondary.body.Config.Name.ToLower(); + return name != primaryName && name != secondaryName && (primary == node.body.Config.Name.ToLower() || primary == primaryName || primary == secondaryName); + } + + + private static PlanetNode ConstructGraph(NewHorizonsBody body, NewHorizonsBody[] bodies) + { + if (body.Config.FocalPoint == null) + { + return new PlanetNode + { + body = body, + children = bodies + .Where(b => string.Equals(b.Config.Orbit.PrimaryBody, body.Config.Name, StringComparison.CurrentCultureIgnoreCase)) + .Select(b => ConstructGraph(b, bodies)) + }; + } + else + { + var newNode = new FocalPointNode + { + body = body + }; + foreach (var child in bodies) + { + if (string.Equals(child.Config.Name, body.Config.FocalPoint.Primary, StringComparison.CurrentCultureIgnoreCase)) + { + newNode.primary = new PlanetNode + { + body = child, + children = new List() + }; + } + else if (string.Equals(child.Config.Name, body.Config.FocalPoint.Secondary, StringComparison.CurrentCultureIgnoreCase)) + { + newNode.secondary = new PlanetNode + { + body = child, + children = new List() + }; + } + } + newNode.children = bodies + .Where(b => DetermineIfChildOfFocal(b, newNode)) + .Select(b => ConstructGraph(b, bodies)); + return newNode; + } + } + + public IEnumerator GetEnumerator() + { + yield return _rootNode; + var queue = new Queue(_rootNode.children); + while (queue.Count != 0) + { + var node = queue.Dequeue(); + yield return node; + foreach (var child in node.children) + { + queue.Enqueue(child); + } + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } +} \ No newline at end of file diff --git a/NewHorizons/Patches/RaftPatches.cs b/NewHorizons/Patches/RaftPatches.cs index c6cb2c03..663d6359 100644 --- a/NewHorizons/Patches/RaftPatches.cs +++ b/NewHorizons/Patches/RaftPatches.cs @@ -91,7 +91,7 @@ namespace NewHorizons.Patches [HarmonyPatch(typeof(AlignToSurfaceFluidDetector), "ManagedFixedUpdate")] public static bool AlignToSurfaceFluidDetector_ManagedFixedUpdate(AlignToSurfaceFluidDetector __instance) { - if (!__instance._alignmentFluid is NHFluidVolume) return true; + // if (!__instance._alignmentFluid is NHFluidVolume) return true; // Mostly copy pasting from the AlignWithDirection class AsymmetricFluidDetector_ManagedFixedUpdate(__instance);