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 66cd896
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Source/ProceduralFairings/FairingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,17 @@ public enum BaseMode { Payload, Adapter, Plate, Other }
float lastHeight = -1000;
float lastExtraHt = -1000;

// for non-inline fairings, the "real" maximum width of the fairing, the bulge in the middle
float maxFairingSize = 0;

[KSPField] public bool requestLegacyLoad;

public int TopNodeSize => Mathf.RoundToInt((Mode == BaseMode.Adapter ? topSize : baseSize) / diameterStepLarge);
public int BottomNodeSize => Mathf.RoundToInt(baseSize / diameterStepLarge);
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 @@ -537,12 +540,16 @@ public void UpdateShape(bool pushAttachments)

Profiler.BeginSample("PF.UpdateShape.Setup");
SetUIFieldLimits();
Profiler.BeginSample("PF.RecalcShape.ScanPayload");
var 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 +1130,24 @@ private void FillProfileOutline(PayloadScan scan)
}
}

private void RecalcMaxSize(PayloadScan scan)
{
if (Mode != BaseMode.Payload)
{
maxFairingSize = baseSize;
}
else if (!autoShape)
{
maxFairingSize = manualMaxSize;
}
maxFairingSize = Mathf.Max(scan.profile.Take(scan.profile.Count).ToArray()) * 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 66cd896

Please sign in to comment.