diff --git a/Source/Tanks/ModuleFuelTanks.cs b/Source/Tanks/ModuleFuelTanks.cs index 952e1985..32643b87 100644 --- a/Source/Tanks/ModuleFuelTanks.cs +++ b/Source/Tanks/ModuleFuelTanks.cs @@ -1,14 +1,13 @@ using System; using System.Collections.Generic; -using System.Collections; -using System.Linq; +using System.Reflection; +using UniLinq; using UnityEngine; +using UnityEngine.Profiling; using KSP.UI.Screens; -using System.Reflection; using KSP.Localization; using ROUtils; -using UnityEngine.Profiling; // ReSharper disable InconsistentNaming, CompareOfFloatsByEqualityOperator @@ -57,6 +56,8 @@ public void ChooseTankDefinition() } private TankDefinitionSelectionGUI tankDefinitionSelectionGUI = null; + private bool onLoadFiredInEditor; + // The total tank volume. This is prior to utilization public double totalVolume; @@ -238,6 +239,8 @@ public override void OnLoad(ConfigNode node) // Setup the mass massDirty = true; CalculateMass(); + + onLoadFiredInEditor = HighLogic.LoadedSceneIsEditor; } OnLoadRF(node); } @@ -280,6 +283,7 @@ public override void OnStart(StartState state) if (MFSSettings.previewAllLockedTypes) GatherLockedTypesFromAllPossible(); InitializeTankType(); + TrySetBestTankDef(); UpdateTankType(false); InitUtilization(); Fields[nameof(utilization)].uiControlEditor.onFieldChanged += OnUtilizationChanged; @@ -375,6 +379,21 @@ private void InitializeTankType() c.display = typesAvailable.Select(t => ConstructColoredTypeTitle(t)).ToArray(); } + private void TrySetBestTankDef() + { + if (!onLoadFiredInEditor) + { + var match = typesAvailable + .Where(t => t.orderOfPreference >= 0 && !lockedTypes.Contains(t)) + .OrderByDescending((e) => e.orderOfPreference) + .FirstOrDefault(); + if (match != null) + { + type = match.name; + } + } + } + private string ConstructColoredTypeTitle(TankDefinition def) { if (!MFSSettings.previewAllLockedTypes || HighLogic.LoadedScene == GameScenes.LOADING) diff --git a/Source/Tanks/TankDefinition.cs b/Source/Tanks/TankDefinition.cs index bb7d8919..26703752 100644 --- a/Source/Tanks/TankDefinition.cs +++ b/Source/Tanks/TankDefinition.cs @@ -35,6 +35,13 @@ public class TankDefinition : IConfigNode [Persistent] public float maxUtilization = 0; + /// + /// When picking a new part from catalog, the definition with highest preference number will be set as the default type. + /// Use -1 to prevent a definition from being chosen. If all definitions have -1 then no preference-based override will happen. + /// + [Persistent] + public int orderOfPreference = -1; + public Dictionary tankList = new Dictionary(); public List tags = new List();