Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ResetIgnitions() method to MERF #336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Source/Engines/ModuleEngineConfigs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public class ModuleEngineConfigsBase : PartModule, IPartCostModifier, IPartMassM
public float origMass = -1;
protected float massDelta = 0;

public int? Ignitions { get; protected set; }

[KSPField]
public string gimbalTransform = string.Empty;
[KSPField]
Expand Down Expand Up @@ -824,15 +826,18 @@ protected void SetConfiguration(ConfigNode newConfig, bool resetTechLevels)
DoConfig(config);

HandleEngineIgnitor(config);

Ignitions = null;
if (config.HasValue("ignitions"))
{
if (HighLogic.LoadedSceneIsFlight && vessel?.situation != Vessel.Situations.PRELAUNCH)
config.RemoveValue("ignitions");
else if (int.TryParse(config.GetValue("ignitions"), out int ignitions))
if (int.TryParse(config.GetValue("ignitions"), out int tmpIgnitions))
{
ignitions = ConfigIgnitions(ignitions);
config.SetValue("ignitions", ignitions);
Ignitions = ConfigIgnitions(tmpIgnitions);
config.SetValue("ignitions", Ignitions.Value);
}

if (HighLogic.LoadedSceneIsFlight && vessel?.situation != Vessel.Situations.PRELAUNCH)
config.RemoveValue("ignitions");
}

// Trigger re-computation of the response rate if one is not set explicitly.
Expand Down
19 changes: 19 additions & 0 deletions Source/Engines/ModuleEnginesRF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,25 @@ public void SetScale(double newScale)
scale = newScale;
rfSolver?.SetScale(scale);
}

/// <summary>
/// Used by the engine repair feature in RP-1.
/// </summary>
public void ResetIgnitions()
{
List<ModuleEngineConfigs> mecs = part.FindModulesImplementing<ModuleEngineConfigs>();
ModuleEngineConfigs matchingMEC = mecs.Find(m => m.pModule == this);
if (matchingMEC != null && matchingMEC.Ignitions.HasValue)
{
ignitions = matchingMEC.Ignitions.Value;
}
else
{
ModuleEnginesRF merf = part.FetchModuleFromPrefab(this);
if (merf != null)
ignitions = merf.ignitions;
}
}
#endregion

#region Info
Expand Down
10 changes: 1 addition & 9 deletions Source/Tanks/ModuleFuelTanks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,7 @@ public override void OnAwake()
unmanagedResources = new Dictionary<string, UnmanagedResource>();
else if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor)
{
int index = part.Modules.IndexOf(this);
if (index < 0)
index = part.Modules.Count;
Part prefab = part.partInfo.partPrefab;
ModuleFuelTanks mft;
if (prefab.Modules.Count > index && prefab.Modules[index] is ModuleFuelTanks m)
mft = m;
else
mft = prefab.FindModuleImplementing<ModuleFuelTanks>();
ModuleFuelTanks mft = part.FetchModuleFromPrefab(this);
unmanagedResources = mft.unmanagedResources;
typesAvailable = new List<TankDefinition>(mft.typesAvailable); // Copy so any changes don't impact the prefab
allPossibleTypes = mft.allPossibleTypes;
Expand Down
13 changes: 13 additions & 0 deletions Source/Utilities/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,18 @@ public static void ResolveAndAddUnique(this IList<TankDefinition> defs, IEnumera
ResolveAndAddUnique(defs, name);
}
}

public static T FetchModuleFromPrefab<T>(this Part part, T pm)
where T : PartModule
{
int index = part.Modules.IndexOf(pm);
if (index < 0)
index = part.Modules.Count; // during instantiation (OnAwake), when we are addcomponenting this module, we may not have added it to the Modules list
Part prefab = part.partInfo.partPrefab;
if (prefab.Modules.Count > index && prefab.Modules[index] is T m)
return m;
else
return prefab.FindModuleImplementing<T>();
}
}
}
Loading