diff --git a/Source/RP0/SpaceCenter/Projects/FacilityUpgradeProject.cs b/Source/RP0/SpaceCenter/Projects/FacilityUpgradeProject.cs index 7bbb55fffb5..0c4ebb33571 100644 --- a/Source/RP0/SpaceCenter/Projects/FacilityUpgradeProject.cs +++ b/Source/RP0/SpaceCenter/Projects/FacilityUpgradeProject.cs @@ -29,8 +29,6 @@ public override string GetItemName() return ScenarioUpgradeableFacilities.GetFacilityName(sFacilityType); } - - public FacilityUpgradeProject() { } @@ -59,21 +57,19 @@ public void Abort() public void Apply() { RP0Debug.Log($"Upgrading {name} to level {upgradeLevel}"); - - List facilityRefs = GetFacilityReferencesById(id); - foreach (UpgradeableFacility facility in facilityRefs) - { - facility.SetLevel(upgradeLevel); - } + KCTUtilities.SetFacilityLevel(FacilityType, upgradeLevel); int newLvl = KCTUtilities.GetFacilityLevel(sFacilityType); upgradeProcessed = newLvl == upgradeLevel; if (upgradeProcessed) { UpgradeLockedFacilities(); + RP0Debug.Log($"Upgrade processed: {upgradeProcessed} Current: {newLvl} Desired: {upgradeLevel}"); + } + else + { + RP0Debug.LogError($"Setting facility level failed, Current: {newLvl} Desired: {upgradeLevel}"); } - - RP0Debug.Log($"Upgrade processed: {upgradeProcessed} Current: {newLvl} Desired: {upgradeLevel}"); } public static List GetFacilityReferencesById(string id) @@ -81,12 +77,6 @@ public static List GetFacilityReferencesById(string id) return ScenarioUpgradeableFacilities.protoUpgradeables[id].facilityRefs; } - public static List GetFacilityReferencesByType(SpaceCenterFacility facilityType) - { - string internalId = ScenarioUpgradeableFacilities.SlashSanitize(facilityType.ToString()); - return GetFacilityReferencesById(internalId); - } - public static void UpgradeLockedFacilities() { float avgLevel = 0f; @@ -105,7 +95,6 @@ public static void UpgradeLockedFacilities() avgLevel /= (float)facCount; int desiredLevel = (int)Math.Round(avgLevel * 2d); - List facilityRefs = new List(); for (SpaceCenterFacility fac = SpaceCenterFacility.Administration; fac <= SpaceCenterFacility.VehicleAssemblyBuilding; ++fac) { if (fac == SpaceCenterFacility.Runway || fac == SpaceCenterFacility.LaunchPad) @@ -113,10 +102,8 @@ public static void UpgradeLockedFacilities() if (!Database.LockedFacilities.Contains(fac)) continue; - facilityRefs.AddRange(GetFacilityReferencesByType(fac)); + KCTUtilities.SetFacilityLevel(fac, desiredLevel); } - foreach (var fac in facilityRefs) - fac.SetLevel(desiredLevel); } public bool AlreadyInProgress() @@ -147,7 +134,6 @@ protected override void ProcessCancel() protected override void ProcessComplete() { - if (ScenarioUpgradeableFacilities.Instance != null && !SpaceCenterManagement.Instance.ErroredDuringOnLoad) { Apply(); diff --git a/Source/RP0/Utilities/KCTUtilities.cs b/Source/RP0/Utilities/KCTUtilities.cs index 619ecad5628..20816dd3eb5 100644 --- a/Source/RP0/Utilities/KCTUtilities.cs +++ b/Source/RP0/Utilities/KCTUtilities.cs @@ -1,14 +1,15 @@ -using KSP.UI; +using CommNet; +using KSP.UI; using KSP.UI.Screens; +using ROUtils; using System; using System.Collections.Generic; using System.IO; using System.Reflection; -using System.Text; using UniLinq; using UnityEngine; using UnityEngine.Profiling; -using ROUtils; +using Upgradeables; namespace RP0 { @@ -933,7 +934,7 @@ public static void HandleEditorButton() } } } - else if(SpaceCenterManagement.Instance != null) + else if (SpaceCenterManagement.Instance != null) { InputLockManager.SetControlLock(ControlTypes.EDITOR_LAUNCH, SpaceCenterManagement.KCTLaunchLock); if (!SpaceCenterManagement.Instance.IsLaunchSiteControllerDisabled) @@ -1256,7 +1257,7 @@ public static List SortAndFilterTechListForFinalNodes(HashSet in { HashSet blacklist = new HashSet(); SortedList slist = new SortedList(); - foreach(string s in input) + foreach (string s in input) { foreach (string parent in Database.TechNameToParents[s]) { @@ -1311,7 +1312,7 @@ public static Dictionary GetPartsWithPurchasa } return res; } - + public static void ScrapVessel(VesselProject b) { RP0Debug.Log($"Scrapping {b.shipName}"); @@ -1481,6 +1482,35 @@ public static int GetFacilityLevel(SpaceCenterFacility facility) { return MathUtils.GetIndexFromNorm(ScenarioUpgradeableFacilities.GetFacilityLevel(facility), Database.GetFacilityLevelCount(facility)); } + + public static void SetFacilityLevel(SpaceCenterFacility scf, int level) + { + string facId = ScenarioUpgradeableFacilities.SlashSanitize(scf.ToString()); + ScenarioUpgradeableFacilities.ProtoUpgradeable upgradable = ScenarioUpgradeableFacilities.protoUpgradeables[facId]; + + bool levelWasSet = false; + if (upgradable.facilityRefs.Count > 0) + { + // The facilityRefs are only available when the space center facilities are physically spawned. + // For instance they aren't found in TS scene or when going far enough away from home body. + levelWasSet = true; + foreach (UpgradeableFacility upgd in upgradable.facilityRefs) + { + RP0Debug.Log($"Setting facility {upgd.id} upgrade level through standard path"); + upgd.SetLevel(level); + } + } + + if (!levelWasSet) + { + RP0Debug.Log($"Failed to set facility {scf} upgrade level through standard path, using fallback"); + int maxLevel = Database.GetFacilityLevelCount(scf) - 1; + double normLevel = maxLevel == 0 ? 1d : level / (double)maxLevel; + upgradable.configNode.SetValue("lvl", normLevel); + + // Note that OnKSCFacilityUpgrading and OnKSCFacilityUpgraded events are not fired through this code path + } + } } }