diff --git a/NewHorizons/Components/ShipLogStarChartMode.cs b/NewHorizons/Components/ShipLogStarChartMode.cs index cf5971a1..8bc9e11a 100644 --- a/NewHorizons/Components/ShipLogStarChartMode.cs +++ b/NewHorizons/Components/ShipLogStarChartMode.cs @@ -8,6 +8,7 @@ using NewHorizons.Utility; using Logger = NewHorizons.Utility.Logger; using UnityEngine.UI; using OWML.Common; +using NewHorizons.Handlers; namespace NewHorizons.Components { @@ -33,6 +34,8 @@ namespace NewHorizons.Components private ShipLogEntryCard _target = null; private NotificationData _warpNotificationData = null; + private int _nextCardIndex; + public override void Initialize(ScreenPromptList centerPromptList, ScreenPromptList upperRightPromptList, OWAudioSource oneShotSource) { root = base.transform.Find("ScaleRoot/PanRoot"); @@ -47,25 +50,34 @@ namespace NewHorizons.Components GlobalMessenger.AddListener("TargetReferenceFrame", new Callback(OnTargetReferenceFrame)); GlobalMessenger.AddListener("EnterFlightConsole", new Callback(OnEnterFlightConsole)); - var x = 0; + _nextCardIndex = 0; foreach (var starSystem in Main.BodyDict.Keys) { // Get rid of the warp option for the current system if (starSystem == Main.Instance.CurrentStarSystem) continue; + var config = Main.SystemDict[starSystem]; + // Conditions to allow warping into that system (either no planets (stock system) or has a ship spawn point) var flag = false; if (starSystem.Equals("SolarSystem")) flag = true; - else if (Main.SystemDict[starSystem].Spawn?.ShipSpawnPoint != null) flag = true; + else if (config.Spawn?.ShipSpawnPoint != null) flag = true; + + if (!StarChartHandler.HasUnlockedSystem(starSystem)) continue; if (flag && Main.SystemDict[starSystem].Config.canEnterViaWarpDrive) { - var card = CreateCard(starSystem, root.transform, new Vector2(x++ * 200, 0)); - _starSystemCards.Add(card); + AddSystemCard(starSystem); } } } + public void AddSystemCard(string starSystem) + { + var card = CreateCard(starSystem, root.transform, new Vector2(_nextCardIndex++ * 200, 0)); + _starSystemCards.Add(card); + } + public void OnDestroy() { GlobalMessenger.RemoveListener("TargetReferenceFrame", new Callback(OnTargetReferenceFrame)); diff --git a/NewHorizons/Handlers/StarChartHandler.cs b/NewHorizons/Handlers/StarChartHandler.cs index 6fafab15..45411941 100644 --- a/NewHorizons/Handlers/StarChartHandler.cs +++ b/NewHorizons/Handlers/StarChartHandler.cs @@ -1,10 +1,13 @@ using NewHorizons.Components; +using NewHorizons.External.Configs; +using NewHorizons.Utility; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; +using Logger = NewHorizons.Utility.Logger; namespace NewHorizons.Handlers { @@ -12,8 +15,15 @@ namespace NewHorizons.Handlers { public static ShipLogStarChartMode ShipLogStarChartMode; - public static void Init() + private static Dictionary _starSystemToFactID; + private static Dictionary _factIDToStarSystem; + + private static NewHorizonsSystem[] _systems; + + public static void Init(NewHorizonsSystem[] systems) { + _systems = systems; + var shipLogRoot = GameObject.Find("Ship_Body/Module_Cabin/Systems_Cabin/ShipLogPivot/ShipLog/ShipLogPivot/ShipLogCanvas"); var starChartLog = new GameObject("StarChartMode"); @@ -43,10 +53,67 @@ namespace NewHorizons.Handlers var upperRightPromptList = shipLogRoot.transform.Find("ScreenPromptListScaleRoot/ScreenPromptList_UpperRight")?.GetComponent(); var oneShotSource = GameObject.Find("Ship_Body/Module_Cabin/Systems_Cabin/ShipLogPivot/ShipLog/OneShotAudio_ShipLog")?.GetComponent(); + _starSystemToFactID = new Dictionary(); + _factIDToStarSystem = new Dictionary(); + + foreach (NewHorizonsSystem system in _systems) + { + if (system.Config.factRequiredForWarp != default) + { + RegisterFactForSystem(system.Config.factRequiredForWarp, system.Name); + } + } + ShipLogStarChartMode.Initialize( centerPromptList, upperRightPromptList, oneShotSource); } + + public static bool CanWarp() + { + foreach(var system in _systems) + { + if (system.Config.canEnterViaWarpDrive && system.Spawn?.ShipSpawnPoint != null && HasUnlockedSystem(system.Name)) + { + return true; + } + } + return false; + } + + public static bool HasUnlockedSystem(string system) + { + if (_starSystemToFactID == null || _starSystemToFactID.Count == 0) + return true; + + // If we can't get a fact for the system, then its unlocked + if (!_starSystemToFactID.TryGetValue(system, out var factID)) + return true; + + // If we got a fact but now can't find it elsewhere, its not unlocked + if (!GameObject.FindObjectOfType()._factDict.TryGetValue(factID, out var fact)) + return false; + + // It's unlocked if revealed + return fact.IsRevealed(); + } + + public static void OnRevealFact(string factID) + { + if(_factIDToStarSystem.TryGetValue(factID, out var systemUnlocked)) + { + Logger.Log($"Just learned [{factID}] and unlocked [{systemUnlocked}]"); + if (!Main.HasWarpDrive) Main.Instance.EnableWarpDrive(); + ShipLogStarChartMode.AddSystemCard(systemUnlocked); + } + } + + public static void RegisterFactForSystem(string factID, string system) + { + Logger.Log($"Need to know [{factID}] to unlock [{system}]"); + _starSystemToFactID.Add(system, factID); + _factIDToStarSystem.Add(factID, system); + } } } \ No newline at end of file diff --git a/NewHorizons/Handlers/TranslationHandler.cs b/NewHorizons/Handlers/TranslationHandler.cs index db19631a..e3f6f922 100644 --- a/NewHorizons/Handlers/TranslationHandler.cs +++ b/NewHorizons/Handlers/TranslationHandler.cs @@ -99,10 +99,6 @@ namespace NewHorizons.Handlers if (!_uiTranslationDictionary[language].ContainsKey(key)) _uiTranslationDictionary[language].Add(key, config.UIDictionary[originalKey]); else _uiTranslationDictionary[language][key] = config.UIDictionary[originalKey]; - - //Also add an upper case version - if (!_uiTranslationDictionary[language].ContainsKey(key.ToUpper())) _uiTranslationDictionary[language].Add(key.ToUpper(), config.UIDictionary[originalKey].ToUpper()); - else _uiTranslationDictionary[language][key] = config.UIDictionary[originalKey].ToUpper(); } } } diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 007771bb..2e552218 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -139,6 +139,9 @@ namespace NewHorizons private void ReloadConfigs() { + BodyDict = new Dictionary>(); + SystemDict = new Dictionary(); + BodyDict["SolarSystem"] = new List(); SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(null), this); foreach (AssetBundle bundle in AssetBundles.Values) @@ -196,34 +199,15 @@ namespace NewHorizons // By default we dont have it HasWarpDrive = false; - // Lets us warp home if we want - if (_currentStarSystem != "SolarSystem") - { - HasWarpDrive = true; - } - else - { - // Make the warp controller if there are multiple star systems - foreach (NewHorizonsSystem system in SystemDict.Values) - { - Logger.Log($"System {system}, {system.Config.canEnterViaWarpDrive}"); - if (system.Config.canEnterViaWarpDrive && system.Spawn?.ShipSpawnPoint != null) - { - HasWarpDrive = true; - break; - } - } - } + // Gotta prepare it anyway + StarChartHandler.Init(SystemDict.Values.ToArray()); + HasWarpDrive = StarChartHandler.CanWarp(); - if (HasWarpDrive == true) - { - Logger.Log("Setting up warp drive"); - _shipWarpController = GameObject.Find("Ship_Body").AddComponent(); - _shipWarpController.Init(); - StarChartHandler.Init(); + _shipWarpController = GameObject.Find("Ship_Body").AddComponent(); + _shipWarpController.Init(); + + if (HasWarpDrive == true) EnableWarpDrive(); - LoadBody(LoadConfig(this, "AssetBundle/WarpDriveConfig.json")); - } LoadTranslations(ModHelper.Manifest.ModFolderPath + "AssetBundle/", this); Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => AstroObjectLocator.GetAstroObject("MapSatellite").gameObject.AddComponent()); @@ -330,6 +314,13 @@ namespace NewHorizons IsWarping = false; } + public void EnableWarpDrive() + { + Logger.Log("Setting up warp drive"); + LoadBody(LoadConfig(this, "AssetBundle/WarpDriveConfig.json")); + HasWarpDrive = true; + } + #region TitleScreen public void DisplayBodyOnTitleScreen() @@ -497,7 +488,7 @@ namespace NewHorizons var config = mod.ModHelper.Storage.Load(relativeDirectory); Logger.Log($"Loaded {config.Name}"); if (config.Base.CenterOfSolarSystem) config.Orbit.IsStatic = true; - if (!BodyDict.ContainsKey(config.StarSystem)) + if (!SystemDict.ContainsKey(config.StarSystem)) { // See if theres a star system config var starSystemConfig = mod.ModHelper.Storage.Load($"systems/{config.StarSystem}.json"); diff --git a/NewHorizons/Tools/ShipLogPatches.cs b/NewHorizons/Tools/ShipLogPatches.cs index 2c25b921..b0f990c5 100644 --- a/NewHorizons/Tools/ShipLogPatches.cs +++ b/NewHorizons/Tools/ShipLogPatches.cs @@ -8,6 +8,7 @@ using Logger = NewHorizons.Utility.Logger; using Object = UnityEngine.Object; using NewHorizons.Builder.ShipLog; using NewHorizons.Builder.Handlers; +using NewHorizons.Handlers; namespace NewHorizons.Tools { @@ -29,6 +30,8 @@ namespace NewHorizons.Tools Main.Instance.ModHelper.HarmonyHelper.AddPostfix("Initialize", typeof(ShipLogPatches), nameof(ShipLogPatches.OnShipLogMapModeInitialize)); Main.Instance.ModHelper.HarmonyHelper.AddPostfix("Awake", typeof(ShipLogPatches), nameof(ShipLogPatches.OnShipLogManagerAwakeComplete)); Main.Instance.ModHelper.HarmonyHelper.AddPostfix("UpdateState", typeof(ShipLogPatches), nameof(ShipLogPatches.OnShipLogAstroObjectUpdateState)); + + Main.Instance.ModHelper.HarmonyHelper.AddPostfix(nameof(ShipLogManager.RevealFact), typeof(ShipLogPatches), nameof(ShipLogPatches.OnShipLogManagerRevealFact)); } public static void OnShipLogManagerAwake(ShipLogManager __instance) @@ -211,5 +214,10 @@ namespace NewHorizons.Tools ShipLogManager manager = Locator.GetShipLogManager(); __result = __result.Where(e => manager.GetFact(e) != null).ToList(); } + + public static void OnShipLogManagerRevealFact(string __0) + { + StarChartHandler.OnRevealFact(__0); + } } } \ No newline at end of file