Skip to content

Commit 3ca4134

Browse files
committed
KSP 1.7 recompile + various code changes to ModuleTorqueController to make it fail-safe against vessel/part external in-flight modifications.
1 parent 4ac9941 commit 3ca4134

File tree

6 files changed

+80
-52
lines changed

6 files changed

+80
-52
lines changed
512 Bytes
Binary file not shown.

GameData/MandatoryRCS/MandatoryRCS.version

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@
1111
"VERSION":
1212
{
1313
"MAJOR":1,
14-
"MINOR":5,
14+
"MINOR":6,
1515
"PATCH":0,
1616
"BUILD":0
1717
},
1818
"KSP_VERSION":
1919
{
2020
"MAJOR":1,
21-
"MINOR":4,
22-
"PATCH":1
21+
"MINOR":7,
22+
"PATCH":0
2323
},
2424
"KSP_VERSION_MIN":
2525
{
2626
"MAJOR":1,
27-
"MINOR":4,
28-
"PATCH":1
27+
"MINOR":6,
28+
"PATCH":0
2929
},
3030
"KSP_VERSION_MAX":
3131
{
3232
"MAJOR":1,
33-
"MINOR":4,
34-
"PATCH":1
33+
"MINOR":7,
34+
"PATCH":10
3535
}
3636
}

MandatoryRCS-Source/MandatoryRCS.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
</ItemGroup>
6060
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6161
<PropertyGroup>
62-
<PostBuildEvent>"C:\Users\Got\Desktop\KSP\Tools\pdb2mdb\pdb2mdb.exe" "C:\Users\Got\Desktop\KSP\SPE source\Mod project\MandatoryRCS\MandatoryRCS\MandatoryRCS-Source\bin\Debug\MandatoryRCS.dll"
63-
xcopy /Y "C:\Users\Got\Desktop\KSP\SPE source\Mod project\MandatoryRCS\MandatoryRCS\MandatoryRCS-Source\bin\Debug\MandatoryRCS.dll" "C:\Users\Got\Desktop\KSP\Kerbal Space Program 1.4.1 DEV\GameData\MandatoryRCS\"
64-
xcopy /Y "C:\Users\Got\Desktop\KSP\SPE source\Mod project\MandatoryRCS\MandatoryRCS\MandatoryRCS-Source\bin\Debug\MandatoryRCS.dll.mdb" "C:\Users\Got\Desktop\KSP\Kerbal Space Program 1.4.1 DEV\GameData\MandatoryRCS\"</PostBuildEvent>
62+
<PostBuildEvent>"D:\Projets\KSP\Tools\pdb2mdb\pdb2mdb.exe" "C:\Users\Got\Source\Repos\gotmachine\MandatoryRCS\MandatoryRCS-Source\bin\Debug\MandatoryRCS.dll"
63+
xcopy /Y "C:\Users\Got\Source\Repos\gotmachine\MandatoryRCS\MandatoryRCS-Source\bin\Debug\MandatoryRCS.dll" "D:\Projets\KSP\Kerbal Space Program 1.7.0 DEV\GameData\MandatoryRCS\"
64+
xcopy /Y "C:\Users\Got\Source\Repos\gotmachine\MandatoryRCS\MandatoryRCS-Source\bin\Debug\MandatoryRCS.dll.mdb" "D:\Projets\KSP\Kerbal Space Program 1.7.0 DEV\GameData\MandatoryRCS\"</PostBuildEvent>
6565
</PropertyGroup>
6666
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6767
Other similar extension points exist, see Microsoft.Common.targets.

MandatoryRCS-Source/ModuleTorqueController.cs

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
using System.Text;
55
using UnityEngine;
66

7-
// TODO :
8-
// - Wheels saturation when landed
9-
// - (Maybe) Torque ON when RCS is ON, to reduce RCS fuel consumption
10-
117
namespace MandatoryRCS
128
{
139
public class ModuleTorqueController : PartModule
@@ -38,13 +34,6 @@ public class ModuleTorqueController : PartModule
3834

3935
public void Start()
4036
{
41-
// Get the reaction wheel module
42-
rwmodule = part.Modules.GetModule<ModuleReactionWheel>();
43-
// Get the module config torque value
44-
maxTorque.x = rwmodule.PitchTorque;
45-
maxTorque.y = rwmodule.RollTorque;
46-
maxTorque.z = rwmodule.YawTorque;
47-
4837
if (MandatoryRCSSettings.featureReactionWheels)
4938
{
5039
// Does this RW respond to pilot/SAS input ?
@@ -77,9 +66,16 @@ public void Start()
7766
vessel.OnPreAutopilotUpdate += new FlightInputCallback(GetPilotInput);
7867
callbackIsActive = true;
7968
}
69+
else if (HighLogic.LoadedSceneIsEditor)
70+
{
71+
rwmodule = part.Modules.GetModule<ModuleReactionWheel>();
72+
73+
if (rwmodule == null)
74+
return;
75+
76+
TweakUI(rwmodule);
77+
}
8078
}
81-
// Hide RW control modes and enable/disable toggle from GUI and action groups
82-
TweakUI();
8379
}
8480

8581
private void GetPilotInput(FlightCtrlState st)
@@ -95,14 +91,45 @@ public void OnDestroy()
9591
vessel.OnPreAutopilotUpdate -= new FlightInputCallback(GetPilotInput);
9692
}
9793
}
98-
99-
// Apply torque in OnFlyByWire because the module FixedUpdate() is called too late, resulting in a 1 frame lag in torque updates, leading to having torque when you shouldn't.
94+
95+
// Apply torque in OnFlyByWire because the module FixedUpdate() is called too late,
96+
// resulting in a 1 frame lag in torque updates, leading to having torque when you shouldn't.
10097
private void UpdateTorque(FlightCtrlState st)
10198
{
99+
// autopilot can be null just before vessel is destroyed
100+
if (vessel.Autopilot == null)
101+
return;
102+
103+
// Not sure why but in some cases the reference the the module can be null if acquired in OnStart.
104+
// To be on the safe side we acquire it here
105+
if (rwmodule == null)
106+
{
107+
// Get the reaction wheel module
108+
rwmodule = part.Modules.GetModule<ModuleReactionWheel>();
109+
110+
if (rwmodule == null)
111+
return;
112+
113+
// Get the module config torque value
114+
maxTorque.x = rwmodule.PitchTorque;
115+
maxTorque.y = rwmodule.RollTorque;
116+
maxTorque.z = rwmodule.YawTorque;
117+
118+
// Hide RW control modes and enable/disable toggle from GUI and action groups
119+
TweakUI(rwmodule);
120+
}
121+
122+
// do this only once, and check for null
123+
// note : not caching it because keeping track of vesselModule changes is a mess
124+
VesselModuleRotation vRotModule = vessel.vesselModules.OfType<VesselModuleRotation>().FirstOrDefault();
125+
if (vRotModule == null)
126+
return;
127+
128+
// We have everything we need, now do the nerf
102129
if (FlightGlobals.ready && vessel.loaded && !vessel.packed)
103130
{
104131
// Get the saturation factor calculated from the vessel rotation
105-
saturationFactor = vessel.vesselModules.OfType<VesselModuleRotation>().First().velSaturationTorqueFactor;
132+
saturationFactor = vRotModule.velSaturationTorqueFactor;
106133

107134
// On pilot rotation requests, use nerfed torque output
108135
if (pilotInput)
@@ -122,7 +149,7 @@ private void UpdateTorque(FlightCtrlState st)
122149
// SAS is in target mode, enable full torque if the target is near.
123150
// orientationDiff : 1.0 = toward target, 0.0 = target is at a 90° angle
124151
// float orientationDiff = Math.Max(Vector3.Dot(vessel.Autopilot.SAS.targetOrientation.normalized, vessel.GetTransform().up.normalized), 0);
125-
float orientationDiff = Math.Max(Vector3.Dot(vessel.vesselModules.OfType<VesselModuleRotation>().First().targetDirection.normalized, vessel.GetTransform().up.normalized), 0);
152+
float orientationDiff = Math.Max(Vector3.Dot(vRotModule.targetDirection.normalized, vessel.GetTransform().up.normalized), 0);
126153

127154
if (!isOnTarget && orientationDiff > 0.999f)
128155
{
@@ -208,37 +235,34 @@ private void SetWheelModuleTorque(Vector3 torque)
208235
// Disable all UI buttons and action groups, except the authority limiter slider.
209236
// RW torque can still be tweaked/disabled trough the renamed for clarity "Reaction Wheel Autority" GUI
210237
// TODO : reenable Normal/SAS/Pilot mode switch if strictMode is disabled
211-
private void TweakUI()
238+
private static void TweakUI(ModuleReactionWheel rw)
212239
{
213-
if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor)
240+
foreach (BaseField f in rw.Fields)
214241
{
215-
foreach (BaseField f in rwmodule.Fields)
242+
if (f.name.Equals("actuatorModeCycle") || f.name.Equals("stateString"))
216243
{
217-
if (f.name.Equals("actuatorModeCycle") || f.name.Equals("stateString"))
218-
{
219-
f.guiActive = false;
220-
f.guiActiveEditor = false;
221-
}
222-
if (f.name.Equals("authorityLimiter"))
223-
{
224-
f.guiName = "Reaction Wheel Authority";
225-
}
226-
// Debug.Log("RW Fields : guiName=" + f.guiName + ", name=" + f.name + ", guiActive=" + f.guiActive + ", guiActiveEditor=" + f.guiActiveEditor);
244+
f.guiActive = false;
245+
f.guiActiveEditor = false;
227246
}
228-
foreach (BaseEvent e in rwmodule.Events)
247+
if (f.name.Equals("authorityLimiter"))
229248
{
230-
if (e.name.Equals("OnToggle"))
231-
{
232-
e.guiActive = false;
233-
e.guiActiveEditor = false;
234-
}
235-
// Debug.Log("RW Events : guiName=" + e.guiName + ", name=" + e.name + ", guiActive=" + e.guiActive + ", guiActiveEditor=" + e.guiActiveEditor);
249+
f.guiName = "Reaction Wheel Authority";
236250
}
237-
foreach (BaseAction a in rwmodule.Actions)
251+
// Debug.Log("RW Fields : guiName=" + f.guiName + ", name=" + f.name + ", guiActive=" + f.guiActive + ", guiActiveEditor=" + f.guiActiveEditor);
252+
}
253+
foreach (BaseEvent e in rw.Events)
254+
{
255+
if (e.name.Equals("OnToggle"))
238256
{
239-
a.active = false;
240-
// Debug.Log("RW Actions : guiName=" + a.guiName + ", name=" + a.name + ", active=" + a.active);
257+
e.guiActive = false;
258+
e.guiActiveEditor = false;
241259
}
260+
// Debug.Log("RW Events : guiName=" + e.guiName + ", name=" + e.name + ", guiActive=" + e.guiActive + ", guiActiveEditor=" + e.guiActiveEditor);
261+
}
262+
foreach (BaseAction a in rw.Actions)
263+
{
264+
a.active = false;
265+
// Debug.Log("RW Actions : guiName=" + a.guiName + ", name=" + a.name + ", active=" + a.active);
242266
}
243267
}
244268
}

MandatoryRCS-Source/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.5.0.0")]
36-
[assembly: AssemblyFileVersion("1.5.0.0")]
35+
[assembly: AssemblyVersion("1.6.0.0")]
36+
[assembly: AssemblyFileVersion("1.6.0.0")]

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ So public domain, feel free to do anything, especially updating this plugin if I
7171
- Getting out of timewarps with the SAS direction hold activated input a large roll "kick", most visible at high timewarp levels. I tried a lot of things to find out why this happen or fix it, and failed.
7272
- When switching to an unloaded vessel with its SAS in "target", "antitarget" or "maneuver" mode, the orientation change is applied a few frames after the vessel is unpacked, leading to the rotation event being visible to the player. Won't fix as this is minor, purely cosmetic and fixing would require large modifications.
7373

74+
#### v1.6 for KSP 1.7.0 - 31/05/2019
75+
- Recompiled for KSP 1.7 (should be compatible with KSP 1.6)
76+
- Code tweaks for fail-safe reference acquisition to the stock module. Should fix be the occasional nullref spam that was happening on reentry.
77+
7478
#### v1.5 for KSP 1.4.1
7579
- Recompiled for KSP 1.4.1
7680
- (bugfix) Fixed NRE on asteroids changing SOI

0 commit comments

Comments
 (0)