Skip to content

Commit

Permalink
Fix payload fairing thickness calculation
Browse files Browse the repository at this point in the history
When the middle of the payload fairing is wider than the base, fairing
thickness was incorrectly calculated. This allowed players to
effectively cheese the thickness by making the base arbitrarily small,
allowing for arbitrarily thin fairings.

Now, the fairing thickness should be based on the maximum width of the
fairing instead of the base.

A little bit of refactoring was required to make this work, since
we need the scan data earlier to determine the correct payload fairing
width.
  • Loading branch information
FioraAeterna committed Jun 25, 2023
1 parent 65f80b6 commit 6ab53a4
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions Source/ProceduralFairings/FairingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public enum BaseMode { Payload, Adapter, Plate, Other }
[KSPField] public float decouplerMassMult = 1; // Mass multiplier
[KSPField] public float decouplerMassBase = 0; // Flat additional mass (0.001 = 1kg)

[KSPField] public float maxFairingSize = 0; // the "real" maximum width of the fairing, the bulge in the middle

[KSPField(guiActiveEditor = true, guiName = "Mass", groupName = PFUtils.PAWGroup)]
public string massDisplay;

Expand Down Expand Up @@ -146,7 +148,7 @@ public enum BaseMode { Payload, Adapter, Plate, Other }
public int InterstageNodeSize => Math.Max(1, TopNodeSize - 1);
public int FairingBaseNodeSize => Math.Max(1, TopNodeSize - 1);
public float CalcSideThickness() => Mode == BaseMode.Adapter ? Mathf.Min(sideThickness * Mathf.Max(baseSize, topSize), 0.25f * Mathf.Min(baseSize, topSize))
: Mode == BaseMode.Payload ? baseSize * Mathf.Min(sideThickness, 0.25f)
: Mode == BaseMode.Payload ? Mathf.Max(baseSize, maxFairingSize) * Mathf.Min(sideThickness, 0.25f)
: 0;

public ModifierChangeWhen GetModuleCostChangeWhen() => ModifierChangeWhen.FIXED;
Expand Down Expand Up @@ -533,16 +535,24 @@ private System.Collections.IEnumerator EditorChangeDetector()

public void UpdateShape(bool pushAttachments)
{
PayloadScan scan = default;
Profiler.BeginSample("PF.UpdateShape");

Profiler.BeginSample("PF.UpdateShape.Setup");
SetUIFieldLimits();
if (!HighLogic.LoadedSceneIsFlight)
{
Profiler.BeginSample("PF.RecalcShape.ScanPayload");
scan = ScanPayload();
Profiler.EndSample();
RecalcMaxSize(scan);
}
UpdatePartProperties();
UpdateNodes(pushAttachments);
Profiler.EndSample();
Profiler.BeginSample("PF.UpdateShape.RecalcShape");
if (!HighLogic.LoadedSceneIsFlight)
RecalcShape();
RecalcShape(scan);
Profiler.EndSample();
ProceduralTools.DragCubeTool.UpdateDragCubes(part, stall: DefaultStall);
UpdateFairingSideDragCubes();
Expand Down Expand Up @@ -1123,14 +1133,27 @@ private void FillProfileOutline(PayloadScan scan)
}
}

private void RecalcMaxSize(PayloadScan scan)
{
if (Mode != BaseMode.Payload)
{
maxFairingSize = baseSize;
}
else if (!autoShape)
{
maxFairingSize = manualMaxSize;
}
else
{
maxFairingSize = scan.profile.Max() * 2;
}
}

public bool forcePartPosition = true;
public void RecalcShape ()
private void RecalcShape(PayloadScan scan)
{
if (Mode != BaseMode.Payload && Mode != BaseMode.Adapter)
return;
Profiler.BeginSample("PF.RecalcShape.ScanPayload");
var scan = ScanPayload();
Profiler.EndSample();

// Check for reversed bases (inline fairings).

Expand Down

0 comments on commit 6ab53a4

Please sign in to comment.