Skip to content

Commit

Permalink
Merge pull request #34 from KSP-RO/MultiThrustTransform
Browse files Browse the repository at this point in the history
Allow thrust transforms with different names on the same module
  • Loading branch information
blowfishpro authored Mar 2, 2018
2 parents 3c599f6 + 1b1980a commit d19c94c
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 2 deletions.
Binary file modified GameData/SolverEngines/Plugins/SolverEngines.dll
Binary file not shown.
94 changes: 92 additions & 2 deletions SolverEngines/EngineModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public abstract class ModuleEnginesSolver : ModuleEnginesFX, IModuleInfo, IEngin

protected List<ModuleAnimateHeat> emissiveAnims;

protected List<ThrustTransformInfo> thrustTransformInfos;


// protected internals
protected EngineSolver engineSolver = null;
Expand Down Expand Up @@ -112,6 +114,8 @@ public override void OnStart(PartModule.StartState state)
engineTemp = 288.15d;
currentThrottle = 0f;

InitializeThrustTransforms();

// Get emissives
emissiveAnims = new List<ModuleAnimateHeat>();
int mCount = part.Modules.Count;
Expand All @@ -126,13 +130,99 @@ public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);

thrustTransforms = new List<Transform>(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<ThrustTransformInfo>();

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;
Expand Down
1 change: 1 addition & 0 deletions SolverEngines/SolverEngines.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="PartExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SolverMathUtil.cs" />
<Compile Include="ThrustTransformInfo.cs" />
<Compile Include="ToolbarWrapper.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
56 changes: 56 additions & 0 deletions SolverEngines/ThrustTransformInfo.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
}

0 comments on commit d19c94c

Please sign in to comment.