Skip to content

Commit

Permalink
Apply facility upgrades even when space center is unloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav committed Jun 17, 2024
1 parent 39dc393 commit 6e1a56f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
28 changes: 7 additions & 21 deletions Source/RP0/SpaceCenter/Projects/FacilityUpgradeProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public override string GetItemName()
return ScenarioUpgradeableFacilities.GetFacilityName(sFacilityType);
}



public FacilityUpgradeProject()
{
}
Expand Down Expand Up @@ -59,34 +57,26 @@ public void Abort()
public void Apply()
{
RP0Debug.Log($"Upgrading {name} to level {upgradeLevel}");

List<UpgradeableFacility> 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<UpgradeableFacility> GetFacilityReferencesById(string id)
{
return ScenarioUpgradeableFacilities.protoUpgradeables[id].facilityRefs;
}

public static List<UpgradeableFacility> GetFacilityReferencesByType(SpaceCenterFacility facilityType)
{
string internalId = ScenarioUpgradeableFacilities.SlashSanitize(facilityType.ToString());
return GetFacilityReferencesById(internalId);
}

public static void UpgradeLockedFacilities()
{
float avgLevel = 0f;
Expand All @@ -105,18 +95,15 @@ public static void UpgradeLockedFacilities()
avgLevel /= (float)facCount;
int desiredLevel = (int)Math.Round(avgLevel * 2d);

List<UpgradeableFacility> facilityRefs = new List<UpgradeableFacility>();
for (SpaceCenterFacility fac = SpaceCenterFacility.Administration; fac <= SpaceCenterFacility.VehicleAssemblyBuilding; ++fac)
{
if (fac == SpaceCenterFacility.Runway || fac == SpaceCenterFacility.LaunchPad)
continue;
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()
Expand Down Expand Up @@ -147,7 +134,6 @@ protected override void ProcessCancel()

protected override void ProcessComplete()
{

if (ScenarioUpgradeableFacilities.Instance != null && !SpaceCenterManagement.Instance.ErroredDuringOnLoad)
{
Apply();
Expand Down
42 changes: 36 additions & 6 deletions Source/RP0/Utilities/KCTUtilities.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1256,7 +1257,7 @@ public static List<string> SortAndFilterTechListForFinalNodes(HashSet<string> in
{
HashSet<string> blacklist = new HashSet<string>();
SortedList<string, string> slist = new SortedList<string, string>();
foreach(string s in input)
foreach (string s in input)
{
foreach (string parent in Database.TechNameToParents[s])
{
Expand Down Expand Up @@ -1311,7 +1312,7 @@ public static Dictionary<AvailablePart, PartPurchasability> GetPartsWithPurchasa
}
return res;
}

public static void ScrapVessel(VesselProject b)
{
RP0Debug.Log($"Scrapping {b.shipName}");
Expand Down Expand Up @@ -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
}
}
}
}

Expand Down

0 comments on commit 6e1a56f

Please sign in to comment.