Skip to content

Commit 675b2d5

Browse files
committed
Quick and dirty port of the #118 PR against the current codebase.
Removed the alternative attitude controller (was a port of the mechjeb attitude controller), this is outside of the KSPCF scope. Ported the stock SAS enhancements (this need to be tested a bit more, I'm starting to doubt there is any positive difference) Optimized the ModuleRCS.GetPotentialTorque replacement An optimization pass for the other modules would be nice. Also would be a good idea to cache the vessel localtoworld and worldtolocal matrices between GetPotentialTorque calls on separate modules.
1 parent 8571965 commit 675b2d5

File tree

9 files changed

+2568
-39
lines changed

9 files changed

+2568
-39
lines changed

GameData/KSPCommunityFixes/Settings.cfg

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ KSP_COMMUNITY_FIXES
6363
// Fix a bug causing the ROC manager to crash during loading with Kopernicus modified systems
6464
ROCValidationOOR = true
6565

66-
// Fix reaction wheels reporting incorrect available torque when the "Wheel Authority"
67-
// tweakable is set below 100%. This fix instability issues with the stock SAS and other
68-
// attitude controllers from various mods.
69-
ReactionWheelsPotentialTorque = true
70-
7166
// Make the stock alarm to respect the day/year length defined by mods like
7267
// Kronometer. Fix the underlying AppUIMemberDateTime UI widget API to use the
7368
// custom IDateTimeFormatter if implemented.
@@ -233,6 +228,11 @@ KSP_COMMUNITY_FIXES
233228
// Fix exception spam when a radiator set to `parentCoolingOnly` is detached from the vessel
234229
ModuleActiveRadiatorNoParentException = true
235230
231+
// Rewrite of the stock ITorqueProvider.GetPotentialTorque() implementations for the reaction wheels,
232+
// rcs, gimbal and control surface stock partmodules. Fix various issues with the stock implementations,
233+
// ranging from "minor" to "completely broken".
234+
GetPotentialTorqueFixes = true
235+
236236
// ##########################
237237
// Obsolete bugfixes
238238
// ##########################
@@ -311,6 +311,12 @@ KSP_COMMUNITY_FIXES
311311
// Allow targeting the parent body of the current craft, or any body in the parent hierarchy
312312
TargetParentBody = true
313313
314+
// Add two extra tweakables in the RCS module "Actuation Toggles", giving the ability to define
315+
// a separate angle threshold for linear and rotation actuation. This allow to optimize efficiency
316+
// of multi-nozzle RCS parts that are impossible to fine-tune with only the actuation toggles.
317+
// This also add a potential torque/force readout to the actuation toggles PAW items.
318+
RCSLimiter = true
319+
314320
// ##########################
315321
// Performance tweaks
316322
// ##########################
@@ -478,6 +484,12 @@ KSP_COMMUNITY_FIXES
478484
// Significantly reduces the time it takes to open the craft browser and to search by name. Most noticeable with lots of craft.
479485
CraftBrowserOptimisations = true
480486

487+
// Slightly improves the stock SAS precision for vessels with assymetric torque capabilities
488+
BetterSAS = true
489+
490+
// Disable control surfaces updates and actuation when the part isn't submerged or in an atmosphere
491+
NoLiftInSpace = true
492+
481493
// ##########################
482494
// Modding
483495
// ##########################

KSPCommunityFixes/BugFixes/GetPotentialTorqueFixes.cs

Lines changed: 1421 additions & 0 deletions
Large diffs are not rendered by default.

KSPCommunityFixes/BugFixes/ReactionWheelsPotentialTorque.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

KSPCommunityFixes/KSPCommunityFixes.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Compile Include="BugFixes\CorrectDragForFlags.cs" />
129129
<Compile Include="BugFixes\DragCubeLoadException.cs" />
130130
<Compile Include="BugFixes\EVAConstructionMass.cs" />
131+
<Compile Include="BugFixes\GetPotentialTorqueFixes.cs" />
131132
<Compile Include="BugFixes\InventoryPartMass.cs" />
132133
<Compile Include="BugFixes\ModuleActiveRadiatorNoParentException.cs" />
133134
<Compile Include="BugFixes\ModulePartVariantsNodePersistence.cs" />
@@ -150,6 +151,7 @@
150151
<Compile Include="BugFixes\ZeroCostTechNodes.cs" />
151152
<Compile Include="Library\Collections\Deque.cs" />
152153
<Compile Include="Library\Collections\FastStack.cs" />
154+
<Compile Include="Library\EditorPhysics.cs" />
153155
<Compile Include="Library\Extensions.cs" />
154156
<Compile Include="Library\KSPObjectsExtensions.cs" />
155157
<Compile Include="Library\LocalizationUtils.cs" />
@@ -166,6 +168,7 @@
166168
<Compile Include="BugFixes\DoubleCurvePreserveTangents.cs" />
167169
<Compile Include="BugFixes\RestoreMaxPhysicsDT.cs" />
168170
<Compile Include="Performance\FloatingOriginPerf.cs" />
171+
<Compile Include="Performance\NoLiftInSpace.cs" />
169172
<Compile Include="Performance\OptimisedVectorLines.cs" />
170173
<Compile Include="Performance\GameDatabasePerf.cs" />
171174
<Compile Include="Performance\PartSystemsFastUpdate.cs" />
@@ -200,7 +203,6 @@
200203
<Compile Include="BugFixes\PartListTooltipIconSpin.cs" />
201204
<Compile Include="BugFixes\PartStartStability.cs" />
202205
<Compile Include="BugFixes\PAWItemOrder.cs" />
203-
<Compile Include="BugFixes\ReactionWheelsPotentialTorque.cs" />
204206
<Compile Include="BugFixes\RoboticsDrift.cs" />
205207
<Compile Include="BugFixes\ROCValidationOOR.cs" />
206208
<Compile Include="BugFixes\ScatterDistribution.cs" />
@@ -220,7 +222,9 @@
220222
<Compile Include="Performance\ProgressTrackingSpeedBoost.cs" />
221223
<Compile Include="QoL\AutostrutActions.cs" />
222224
<Compile Include="QoL\BetterEditorUndoRedo.cs" />
225+
<Compile Include="QoL\BetterSAS.cs" />
223226
<Compile Include="QoL\OptionalMakingHistoryDLCFeatures.cs" />
227+
<Compile Include="QoL\RCSLimiter.cs" />
224228
<Compile Include="QoL\TargetParentBody.cs" />
225229
<Compile Include="QoL\ToolbarShowHide.cs" />
226230
<Compile Include="QoL\DisableNewGameIntro.cs" />
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using HarmonyLib;
4+
using UnityEngine;
5+
6+
namespace KSPCommunityFixes
7+
{
8+
public class EditorPhysics : BasePatch
9+
{
10+
protected override bool IgnoreConfig => true;
11+
12+
protected override Version VersionMin => new Version(1, 12, 3);
13+
14+
protected override void ApplyPatches()
15+
{
16+
instance = this;
17+
18+
AddPatch(PatchType.Postfix, typeof(DeltaVAppSituation), nameof(DeltaVAppSituation.UpdatePressureDisplay));
19+
20+
GameEvents.onEditorShipModified.Add(OnEditorShipModified);
21+
GameEvents.onDeltaVAppAtmosphereChanged.Add(OnDeltaVAppAtmosphereChanged);
22+
GameEvents.OnControlPointChanged.Add(OnControlPointChanged);
23+
GameEvents.onGameSceneLoadRequested.Add(OnGameSceneLoadRequested);
24+
}
25+
26+
private static EditorPhysics instance;
27+
public static bool TryGetAndUpdate(out EditorPhysics updatedInstance)
28+
{
29+
instance.Update();
30+
if (instance.isValid)
31+
{
32+
updatedInstance = instance;
33+
return true;
34+
}
35+
36+
updatedInstance = null;
37+
return false;
38+
}
39+
40+
private bool isValid;
41+
42+
public Vector3 CoM => EditorMarker_CoM.CraftCoM;
43+
public Transform referenceTransform;
44+
public Part referencePart;
45+
public int referencePartShipIndex;
46+
47+
public CelestialBody body;
48+
public double atmStaticPressureKpa;
49+
public double atmStaticPressure;
50+
public double atmDensity;
51+
public double atmTemperature;
52+
53+
public int lastShipModificationFrame = int.MaxValue;
54+
private int lastShipStatsUpdateFrame;
55+
56+
private void Update()
57+
{
58+
if (lastShipStatsUpdateFrame < lastShipModificationFrame)
59+
{
60+
lastShipStatsUpdateFrame = lastShipModificationFrame;
61+
EditorUpdateShipStats();
62+
}
63+
}
64+
65+
private void EditorUpdateShipStats()
66+
{
67+
if (EditorLogic.fetch.ship == null || EditorLogic.fetch.ship.parts.Count == 0)
68+
{
69+
isValid = false;
70+
return;
71+
}
72+
73+
if (DeltaVGlobals.fetch != null && DeltaVGlobals.ready && DeltaVGlobals.DeltaVAppValues.body != null)
74+
{
75+
body = DeltaVGlobals.DeltaVAppValues.body;
76+
77+
if (!DeltaVGlobals.DeltaVAppValues.body.atmosphere || DeltaVGlobals.DeltaVAppValues.situation == DeltaVSituationOptions.Vaccum)
78+
{
79+
80+
atmStaticPressureKpa = 0.0;
81+
atmStaticPressure = 0.0;
82+
atmTemperature = 0.0;
83+
atmDensity = 0.0;
84+
}
85+
else
86+
{
87+
if (DeltaVGlobals.DeltaVAppValues.situation == DeltaVSituationOptions.Altitude)
88+
{
89+
atmTemperature = body.GetFullTemperature(DeltaVGlobals.DeltaVAppValues.altitude, 0.0);
90+
atmStaticPressureKpa = body.GetPressure(DeltaVGlobals.DeltaVAppValues.altitude);
91+
}
92+
else
93+
{
94+
atmTemperature = body.GetFullTemperature(0.0, 0.0);
95+
atmStaticPressureKpa = body.GetPressure(0.0);
96+
}
97+
98+
atmStaticPressure = atmStaticPressureKpa * 0.0098692326671601278;
99+
atmDensity = DeltaVGlobals.DeltaVAppValues.atmDensity;
100+
}
101+
}
102+
else
103+
{
104+
atmStaticPressureKpa = 0.0;
105+
atmStaticPressure = 0.0;
106+
atmTemperature = 0.0;
107+
atmDensity = 0.0;
108+
}
109+
110+
if (referenceTransform.IsNullOrDestroyed() || EditorLogic.fetch.ship.Parts.IndexOf(referencePart) != referencePartShipIndex)
111+
{
112+
if (!GetFirstReferenceTransform(EditorLogic.RootPart, ref referenceTransform, ref referencePart))
113+
{
114+
referencePart = EditorLogic.RootPart;
115+
referenceTransform = EditorLogic.RootPart.referenceTransform;
116+
}
117+
118+
referencePartShipIndex = EditorLogic.fetch.ship.Parts.IndexOf(referencePart);
119+
}
120+
121+
if (referenceTransform.IsNullOrDestroyed())
122+
{
123+
isValid = false;
124+
return;
125+
}
126+
127+
bool GetFirstReferenceTransform(Part part, ref Transform referenceTransform, ref Part referencePart)
128+
{
129+
if (part.isControlSource != Vessel.ControlLevel.NONE)
130+
{
131+
ModuleCommand mc = part.FindModuleImplementing<ModuleCommand>();
132+
if (mc != null && mc.controlPoints != null && mc.controlPoints.TryGetValue(mc.activeControlPointName, out ControlPoint ctrlPoint))
133+
referenceTransform = ctrlPoint.transform;
134+
else
135+
referenceTransform = part.referenceTransform;
136+
137+
referencePart = part;
138+
return true;
139+
}
140+
141+
int childIdx = part.children.Count;
142+
while (childIdx-- > 0)
143+
{
144+
if (GetFirstReferenceTransform(part.children[childIdx], ref referenceTransform, ref referencePart))
145+
return true;
146+
}
147+
148+
return false;
149+
}
150+
151+
EditorMarker_CoM comMarker = EditorVesselOverlays.fetch.CoMmarker;
152+
if (comMarker == null)
153+
{
154+
isValid = false;
155+
return;
156+
}
157+
158+
if (!comMarker.isActiveAndEnabled)
159+
{
160+
comMarker.rootPart = EditorLogic.RootPart;
161+
comMarker.UpdatePosition();
162+
}
163+
164+
isValid = true;
165+
}
166+
167+
private void OnGameSceneLoadRequested(GameScenes data)
168+
{
169+
referenceTransform = null;
170+
referencePart = null;
171+
referencePartShipIndex = -1;
172+
atmStaticPressure = 0f;
173+
lastShipModificationFrame = 0;
174+
lastShipStatsUpdateFrame = 0;
175+
}
176+
177+
private void OnControlPointChanged(Part part, ControlPoint controlPoint)
178+
{
179+
if (HighLogic.LoadedScene != GameScenes.EDITOR || EditorLogic.fetch.ship == null)
180+
return;
181+
182+
int partIndex = EditorLogic.fetch.ship.Parts.IndexOf(part);
183+
if (partIndex < 0)
184+
return;
185+
186+
lastShipModificationFrame = Time.frameCount;
187+
referenceTransform = controlPoint.transform;
188+
referencePart = part;
189+
referencePartShipIndex = partIndex;
190+
}
191+
192+
private void OnDeltaVAppAtmosphereChanged(DeltaVSituationOptions data)
193+
{
194+
lastShipModificationFrame = Time.frameCount;
195+
}
196+
197+
private void OnEditorShipModified(ShipConstruct ship)
198+
{
199+
if (ship != EditorLogic.fetch.ship || ship.parts.Count == 0)
200+
return;
201+
202+
lastShipModificationFrame = Time.frameCount;
203+
}
204+
205+
// OnDeltaVAppAtmosphereChanged isn't fired when the altitude is modified, so implement our own event
206+
static void DeltaVAppSituation_UpdatePressureDisplay_Postfix()
207+
{
208+
instance.lastShipModificationFrame = Time.frameCount;
209+
}
210+
}
211+
}

KSPCommunityFixes/Library/Numerics.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,5 +788,12 @@ public Vector3d MultiplyVector(double x, double y, double z)
788788
m10 * x + m11 * y + m12 * z,
789789
m20 * x + m21 * y + m22 * z);
790790
}
791+
792+
public Vector3d Right => new Vector3d(m00, m10, m20);
793+
public Vector3d Left => new Vector3d(-m00, -m10, -m20);
794+
public Vector3d Up => new Vector3d(m01, m11, m21);
795+
public Vector3d Down => new Vector3d(-m01, -m11, -m21);
796+
public Vector3d Forward => new Vector3d(m02, m12, m22);
797+
public Vector3d Back => new Vector3d(-m02, -m12, -m22);
791798
}
792799
}

0 commit comments

Comments
 (0)