diff --git a/NewHorizons/Builder/ShipLog/MapModeBuilder.cs b/NewHorizons/Builder/ShipLog/MapModeBuilder.cs index 3cb96b34..7913453c 100644 --- a/NewHorizons/Builder/ShipLog/MapModeBuilder.cs +++ b/NewHorizons/Builder/ShipLog/MapModeBuilder.cs @@ -16,13 +16,13 @@ namespace NewHorizons.Builder.ShipLog public static class MapModeBuilder { #region General - public static ShipLogAstroObject[][] ConstructMapMode(string systemName, GameObject transformParent, int layer) + public static ShipLogAstroObject[][] ConstructMapMode(string systemName, GameObject transformParent, ShipLogAstroObject[][] currentNav, int layer) { Material greyScaleMaterial = GameObject.Find(ShipLogHandler.PAN_ROOT_PATH + "/TimberHearth/Sprite").GetComponent().material; List bodies = Main.BodyDict[systemName].Where(b => (b.Config.ShipLog?.mapMode?.remove ?? false) == false).ToList(); - bool flagManualPositionUsed = false; + bool flagManualPositionUsed = systemName == "SolarSystem"; bool flagAutoPositionUsed = false; - foreach (NewHorizonsBody body in bodies) + foreach (NewHorizonsBody body in bodies.Where(b => ShipLogHandler.IsVanillaBody(b) == false)) { if (body.Config.ShipLog?.mapMode?.manualPosition == null) { @@ -50,7 +50,7 @@ namespace NewHorizons.Builder.ShipLog } else if (flagManualPositionUsed) { - return ConstructMapModeManual(bodies, transformParent, greyScaleMaterial, layer); + return ConstructMapModeManual(bodies, transformParent, greyScaleMaterial, currentNav, layer); } return null; } @@ -178,29 +178,77 @@ namespace NewHorizons.Builder.ShipLog #endregion #region Manual Map Mode - private static ShipLogAstroObject[][] ConstructMapModeManual(List bodies, GameObject transformParent, Material greyScaleMaterial, int layer) + private static ShipLogAstroObject[][] ConstructMapModeManual(List bodies, GameObject transformParent, Material greyScaleMaterial, ShipLogAstroObject[][] currentNav, int layer) { - int maxAmount = bodies.Count; + int maxAmount = bodies.Count + 20; ShipLogAstroObject[][] navMatrix = new ShipLogAstroObject[maxAmount][]; for (int i = 0; i < maxAmount; i++) { navMatrix[i] = new ShipLogAstroObject[maxAmount]; } - foreach (NewHorizonsBody body in bodies) + Dictionary astroIdToNavIndex = new Dictionary(); + + if (Main.Instance.CurrentStarSystem == "SolarSystem") + { + + for (int y = 0; y < currentNav.Length; y++) + { + for (int x = 0; x < currentNav[y].Length; x++) + { + navMatrix[y][x] = currentNav[y][x]; + astroIdToNavIndex.Add(currentNav[y][x].GetID(), new [] {y, x}); + } + } + } + + foreach (NewHorizonsBody body in bodies.Where(b => ShipLogHandler.IsVanillaBody(b) == false)) { - if (ShipLogHandler.IsVanillaBody(body)) continue; GameObject newMapModeGO = CreateMapModeGameObject(body, transformParent, layer, body.Config.ShipLog?.mapMode?.manualPosition); ShipLogAstroObject newAstroObject = AddShipLogAstroObject(newMapModeGO, body, greyScaleMaterial, layer); MakeDetails(body, newMapModeGO.transform, greyScaleMaterial); Vector2 navigationPosition = body.Config.ShipLog?.mapMode?.manualNavigationPosition; navMatrix[(int) navigationPosition.y][(int) navigationPosition.x] = newAstroObject; } - - navMatrix = navMatrix.Where(a => a.Count(c => c != null) > 0).Prepend(new ShipLogAstroObject[1]).ToArray(); + + if (Main.Instance.CurrentStarSystem == "SolarSystem") + { + foreach (NewHorizonsBody body in Main.BodyDict["SolarSystem"].Where(ShipLogHandler.IsVanillaBody)) + { + GameObject gameObject = GameObject.Find(ShipLogHandler.PAN_ROOT_PATH + "/" + body.Config.Name.Replace(" ", "")); + if (body.Config.Destroy || (body.Config.ShipLog?.mapMode?.remove ?? false)) + { + ShipLogAstroObject astroObject = gameObject.GetComponent(); + if (astroObject != null) + { + int[] navIndex = astroIdToNavIndex[astroObject.GetID()]; + navMatrix[navIndex[0]][navIndex[1]] = null; + } + Object.Destroy(gameObject); + } + else + { + if (body.Config.ShipLog?.mapMode?.manualPosition != null) + { + gameObject.transform.localPosition = (Vector2)body.Config.ShipLog.mapMode.manualPosition; + } + if (body.Config.ShipLog?.mapMode?.manualNavigationPosition != null) + { + Vector2 navigationPosition = body.Config.ShipLog?.mapMode?.manualNavigationPosition; + navMatrix[(int) navigationPosition.y][(int) navigationPosition.x] = gameObject.GetComponent(); + } + if (body.Config.ShipLog?.mapMode?.scale != null) + { + gameObject.transform.localScale = Vector3.one * body.Config.ShipLog.mapMode.scale; + } + } + } + } + + navMatrix = navMatrix.Where(a => a.Count(c => c != null && c.gameObject != null) > 0).Prepend(new ShipLogAstroObject[1]).ToArray(); for (var index = 0; index < navMatrix.Length; index++) { - navMatrix[index] = navMatrix[index].Where(a => a != null).ToArray(); + navMatrix[index] = navMatrix[index].Where(a => a != null && a.gameObject != null).ToArray(); } return navMatrix; diff --git a/NewHorizons/Handlers/ShipLogHandler.cs b/NewHorizons/Handlers/ShipLogHandler.cs index ef4244f9..2ce83ffe 100644 --- a/NewHorizons/Handlers/ShipLogHandler.cs +++ b/NewHorizons/Handlers/ShipLogHandler.cs @@ -18,10 +18,18 @@ namespace NewHorizons.Builder.Handlers private static Dictionary _astroIdToBody = new Dictionary(); private static string[] vanillaBodies; + private static string[] vanillaIDs; public static void Init() { - vanillaBodies = SearchUtilities.GetAllChildren(GameObject.Find(PAN_ROOT_PATH)).ConvertAll(g => g.name).ToArray(); + List gameObjects = SearchUtilities.GetAllChildren(GameObject.Find(PAN_ROOT_PATH)); + vanillaBodies = gameObjects.ConvertAll(g => g.name).ToArray(); + vanillaIDs = gameObjects.ConvertAll(g => g.GetComponent()?.GetID()).ToArray(); + } + + public static bool IsVanillaAstroID(string astroId) + { + return vanillaIDs.Contains(astroId); } public static bool IsVanillaBody(NewHorizonsBody body) diff --git a/NewHorizons/Tools/ShipLogPatches.cs b/NewHorizons/Tools/ShipLogPatches.cs index 4996366d..7faf1b65 100644 --- a/NewHorizons/Tools/ShipLogPatches.cs +++ b/NewHorizons/Tools/ShipLogPatches.cs @@ -43,6 +43,7 @@ namespace NewHorizons.Tools logEntryLocation._initialized = true; } } + foreach (NewHorizonsBody body in Main.BodyDict[Main.Instance.CurrentStarSystem]) { if (body.Config.ShipLog?.curiosities != null) @@ -50,6 +51,7 @@ namespace NewHorizons.Tools RumorModeBuilder.AddCuriosityColors(body.Config.ShipLog.curiosities); } } + foreach (NewHorizonsBody body in Main.BodyDict[Main.Instance.CurrentStarSystem]) { if (body.Config.ShipLog?.xmlFile != null) @@ -67,6 +69,7 @@ namespace NewHorizons.Tools ShipLogEntry logEntry = __instance._entryList[i]; RumorModeBuilder.UpdateEntryCuriosity(ref logEntry); } + Logger.Log("Ship Log Generation Complete For: " + Main.Instance.CurrentStarSystem, Logger.LogType.Log); } @@ -82,6 +85,7 @@ namespace NewHorizons.Tools { __result = false; } + return false; } @@ -99,6 +103,7 @@ namespace NewHorizons.Tools __instance.RevealFact(fact, false, false); } } + if (Main.Instance.CurrentStarSystem == "SolarSystem") { return true; @@ -112,7 +117,7 @@ namespace NewHorizons.Tools public static bool OnUIStyleManagerGetCuriosityColor(UIStyleManager __instance, CuriosityName __0, bool __1, ref Color __result) { - if ((int)__0 < 7) + if ((int) __0 < 7) { return true; } @@ -125,34 +130,34 @@ namespace NewHorizons.Tools public static void OnShipLogMapModeInitialize(ShipLogMapMode __instance) { - if (Main.Instance.CurrentStarSystem != "SolarSystem") + GameObject panRoot = GameObject.Find(ShipLogHandler.PAN_ROOT_PATH); + GameObject sunObject = GameObject.Find(ShipLogHandler.PAN_ROOT_PATH + "/Sun"); + ShipLogAstroObject[][] navMatrix = MapModeBuilder.ConstructMapMode(Main.Instance.CurrentStarSystem, panRoot, __instance._astroObjects, sunObject.layer); + if (navMatrix == null || navMatrix.Length <= 1) { - GameObject panRoot = GameObject.Find(ShipLogHandler.PAN_ROOT_PATH); - GameObject sunObject = GameObject.Find(ShipLogHandler.PAN_ROOT_PATH + "/Sun"); - ShipLogAstroObject[][] navMatrix = MapModeBuilder.ConstructMapMode(Main.Instance.CurrentStarSystem, panRoot, sunObject.layer); - if (navMatrix == null || navMatrix.Length <= 1) + Logger.LogWarning("Skipping Map Mode Generation."); + } + else + { + __instance._astroObjects = navMatrix; + __instance._startingAstroObjectID = navMatrix[1][0].GetID(); + if (Main.Instance.CurrentStarSystem != "SolarSystem") { - Logger.LogWarning("No planets suitable for map mode found! Defaulting to vanilla menu (expect weirdness!)."); - } - else - { - __instance._astroObjects = navMatrix; - __instance._startingAstroObjectID = navMatrix[1][0].GetID(); List delete = SearchUtilities.GetAllChildren(panRoot).Where(g => g.name.Contains("_ShipLog") == false).ToList(); foreach (GameObject gameObject in delete) { Object.Destroy(GameObject.Find(ShipLogHandler.PAN_ROOT_PATH + "/" + gameObject.name)); } - // Just Lie About Having A Sand Funnel __instance._sandFunnel = __instance.gameObject.AddComponent(); } } + Logger.Log("Map Mode Construction Complete", Logger.LogType.Log); } public static bool OnShipLogAstroObjectGetName(ShipLogAstroObject __instance, ref string __result) { - if (Main.Instance.CurrentStarSystem == "SolarSystem") + if (ShipLogHandler.IsVanillaAstroID(__instance.GetID())) { return true; } @@ -197,5 +202,4 @@ namespace NewHorizons.Tools __result = __result.Where(e => manager.GetFact(e) != null).ToList(); } } -} - \ No newline at end of file +} \ No newline at end of file