Skip to content

Commit

Permalink
Fix decouplers not having mass on initial placement. Allow decouplers…
Browse files Browse the repository at this point in the history
… to use PF-style mass
  • Loading branch information
NathanKell committed Sep 30, 2023
1 parent b175864 commit 28e67f9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 35 additions & 1 deletion Source/DecouplerTweaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ public class DecouplerTweaker : PartModule, IPartMassModifier
[KSPField(isPersistant = true, guiActiveEditor = true, guiName="Mass", guiUnits="T", guiFormat="F3", groupName = ProceduralPart.PAWGroupName)]
public float mass = 0;

[KSPField]
public bool usePFMass = false;
[KSPField]
public Vector4 specificMass = new Vector4(0.0002f, 0.0075f, 0.005f, 0f);
[KSPField]
public float decouplerMassMult = 4f;

private ModuleDecouple decouple;
internal const float ImpulsePerForceUnit = 0.02f;
private ProceduralPart procPart;

public override void OnStart(StartState state)
{
decouple = part.FindModuleImplementing<ModuleDecouple>();
procPart = part.FindModuleImplementing<ProceduralPart>();
if (decouple == null)
{
Debug.LogError($"[ProceduralParts] No ModuleDecouple found on {part}");
Expand All @@ -92,6 +101,16 @@ public override void OnStart(StartState state)
}
}

public override void OnStartFinished(StartState state)
{
base.OnStartFinished(state);
if (HighLogic.LoadedSceneIsEditor && mass == 0f && density > 0)
{
if (procPart != null)
UpdateMass(procPart.Volume);
}
}

public void OnDestroy() => GameEvents.onTimeWarpRateChanged.Remove(OnTimeWarpRateChanged);

private bool updatingTimeWarp = false;
Expand Down Expand Up @@ -142,10 +161,25 @@ private float CalcMaxImpulse(float diam, float min)
public void OnPartVolumeChanged(BaseEventDetails data)
{
if (HighLogic.LoadedSceneIsEditor && density > 0 && data.Get<double>("newTotalVolume") is double volume)
{
UpdateMass(volume);
}
}

private void UpdateMass(double volume)
{
if (usePFMass && procPart != null)
{
float diam = procPart.Diameter;
float baseMass = (((((specificMass.x * diam) + specificMass.y) * diam) + specificMass.z) * diam) + specificMass.w;
mass = baseMass * decouplerMassMult;
mass *= Mathf.Lerp(0.9f, 1.1f, Mathf.InverseLerp(0.1f, 0.3f, procPart.Length / diam));
}
else
{
mass = Convert.ToSingle(density * volume);
GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
}
GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
}
}
}
2 changes: 2 additions & 0 deletions Source/ProceduralPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class ProceduralPart : PartModule, IPartCostModifier, IPartMassModifier
private static bool staticallyResetPartUpgrades = false;
public bool TUEnabled => installedTU && part.GetComponent("KSPTextureSwitch") is Component;
public float Volume => CurrentShape.Volume;
public float Diameter => CurrentShape.MaxDiameter;
public float Length => CurrentShape.Length;

[KSPField] public float diameterMax = float.PositiveInfinity;
[KSPField] public float lengthMax = float.PositiveInfinity;
Expand Down

0 comments on commit 28e67f9

Please sign in to comment.