diff --git a/GameData/SolverEngines/Plugins/SolverEngines.dll b/GameData/SolverEngines/Plugins/SolverEngines.dll index 64e5b29..3208269 100755 Binary files a/GameData/SolverEngines/Plugins/SolverEngines.dll and b/GameData/SolverEngines/Plugins/SolverEngines.dll differ diff --git a/SolverEngines/EngineModule.cs b/SolverEngines/EngineModule.cs index 3312b95..e75fa01 100644 --- a/SolverEngines/EngineModule.cs +++ b/SolverEngines/EngineModule.cs @@ -55,6 +55,8 @@ public abstract class ModuleEnginesSolver : ModuleEnginesFX, IModuleInfo, IEngin protected List emissiveAnims; + protected List thrustTransformInfos; + // protected internals protected EngineSolver engineSolver = null; @@ -112,6 +114,8 @@ public override void OnStart(PartModule.StartState state) engineTemp = 288.15d; currentThrottle = 0f; + InitializeThrustTransforms(); + // Get emissives emissiveAnims = new List(); int mCount = part.Modules.Count; @@ -126,13 +130,99 @@ public override void OnLoad(ConfigNode node) { base.OnLoad(node); - thrustTransforms = new List(part.FindModelTransforms(thrustVectorTransformName)); // should be done by base, but no harm doing it again. - // -- will be done on Start - CreateEngine(); + if (node.HasNode("THRUST_TRANSFORM")) + { + thrustTransformInfos = new List(); + + foreach (ConfigNode trfNode in node.nodes) + { + if (trfNode.name != "THRUST_TRANSFORM") continue; + + ThrustTransformInfo info; + + try + { + info = new ThrustTransformInfo(trfNode); + thrustTransformInfos.Add(info); + } + catch (Exception e) + { + Debug.LogError($"[{GetType().Name}] exception while attempting to parse THRUST_TRANSFORM: {e}"); + } + } + } + + InitializeThrustTransforms(); CreateEngine(); FitEngineIfNecessary(); } + protected void InitializeThrustTransforms() + { + if (thrustTransformInfos == null && part.partInfo != null && part.partInfo.partPrefab != null && part.partInfo.partPrefab != this) + { + ModuleEnginesSolver prefabModule = null; + + for (int i = 0; i < part.partInfo.partPrefab.Modules.Count; i++) + { + ModuleEnginesSolver m = part.partInfo.partPrefab.Modules[i] as ModuleEnginesSolver; + if (m == null) continue; + if (m.engineID != engineID) continue; + + prefabModule = m; + break; + } + + if (prefabModule == null) + { + Debug.LogError($"[{GetType().Name}] unable to find prefab module"); + return; + } + + thrustTransformInfos = prefabModule.thrustTransformInfos; + } + + if (thrustTransformInfos == null) return; + + thrustTransforms.Clear(); + thrustTransformMultipliers.Clear(); + float normalization = 0; + + foreach(ThrustTransformInfo info in thrustTransformInfos) + { + Transform[] transforms = part.FindModelTransforms(info.transformName); + + if (transforms.Length == 0) + { + Debug.LogError($"[{GetType().Name}] no transforms named {info.transformName} found"); + continue; + } + + if (info.multipliers != null && transforms.Length != info.multipliers.Length) + { + Debug.LogError($"[{GetType().Name}] found {transforms.Length} transforms named {info.transformName} but got {info.multipliers.Length} multipliers"); + continue; + } + + for (int i = 0; i < transforms.Length; i++) + { + thrustTransforms.Add(transforms[i]); + + float multiplier = info.overallMultiplier; + if (info.multipliers != null) multiplier *= info.multipliers[i]; + thrustTransformMultipliers.Add(multiplier); + normalization += multiplier; + } + } + + if (normalization == 0) normalization = 1; + for (int i = 0; i < thrustTransformMultipliers.Count; i++) + { + thrustTransformMultipliers[i] /= normalization; + } + } + new virtual public void FixedUpdate() { realIsp = 0f; diff --git a/SolverEngines/SolverEngines.csproj b/SolverEngines/SolverEngines.csproj index 3138479..be995a5 100644 --- a/SolverEngines/SolverEngines.csproj +++ b/SolverEngines/SolverEngines.csproj @@ -54,6 +54,7 @@ + diff --git a/SolverEngines/ThrustTransformInfo.cs b/SolverEngines/ThrustTransformInfo.cs new file mode 100644 index 0000000..d3c860f --- /dev/null +++ b/SolverEngines/ThrustTransformInfo.cs @@ -0,0 +1,56 @@ +using System; + +namespace SolverEngines +{ + public class ThrustTransformInfo + { + public readonly string transformName; + public readonly float overallMultiplier = 1; + public readonly float[] multipliers = null; + + public ThrustTransformInfo(ConfigNode node) + { + if (node == null) throw new ArgumentNullException(nameof(node)); + + transformName = node.GetValue("name"); + if (string.IsNullOrEmpty(transformName)) throw new ArgumentException("Missing or blank name"); + + string overallMultiplierStr = node.GetValue("overallMultiplier"); + if (!string.IsNullOrEmpty(overallMultiplierStr)) + { + if (!float.TryParse(overallMultiplierStr, out overallMultiplier)) + { + throw new ArgumentException("Could not parse overallMultiplier as float: " + overallMultiplierStr); + } + else if (overallMultiplier <= 0) + { + throw new ArgumentException("overallMultiplier must be positive: " + overallMultiplierStr); + } + } + + if (node.HasValue("multiplier")) + { + string[] strMultipliers = node.GetValues("multiplier"); + + multipliers = new float[strMultipliers.Length]; + for (int i = 0; i < strMultipliers.Length; i++) + { + if (!float.TryParse(strMultipliers[i], out multipliers[i])) + { + throw new ArgumentException("Could not parse multiplier as flooat: " + strMultipliers[i]); + } + else if (multipliers[i] <= 0) + { + throw new ArgumentException("multiplier must be positive: " + strMultipliers[i]); + } + } + + if (multipliers.Length == 1) + { + overallMultiplier *= multipliers[0]; + multipliers = null; + } + } + } + } +}