From 6ab53a45147d55eda4c1e518b9d1b42351abb64d Mon Sep 17 00:00:00 2001 From: Fiora Date: Sun, 25 Jun 2023 22:50:44 +0100 Subject: [PATCH] Fix payload fairing thickness calculation 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. --- Source/ProceduralFairings/FairingBase.cs | 35 ++++++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/Source/ProceduralFairings/FairingBase.cs b/Source/ProceduralFairings/FairingBase.cs index 9814bed..9602802 100644 --- a/Source/ProceduralFairings/FairingBase.cs +++ b/Source/ProceduralFairings/FairingBase.cs @@ -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; @@ -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; @@ -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(); @@ -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).