Skip to content

Commit

Permalink
KSP 1.7 recompile + various code changes to ModuleTorqueController to…
Browse files Browse the repository at this point in the history
… make it fail-safe against vessel/part external in-flight modifications.
  • Loading branch information
gotmachine committed May 31, 2019
1 parent 4ac9941 commit 3ca4134
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 52 deletions.
Binary file modified GameData/MandatoryRCS/MandatoryRCS.dll
Binary file not shown.
14 changes: 7 additions & 7 deletions GameData/MandatoryRCS/MandatoryRCS.version
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
"VERSION":
{
"MAJOR":1,
"MINOR":5,
"MINOR":6,
"PATCH":0,
"BUILD":0
},
"KSP_VERSION":
{
"MAJOR":1,
"MINOR":4,
"PATCH":1
"MINOR":7,
"PATCH":0
},
"KSP_VERSION_MIN":
{
"MAJOR":1,
"MINOR":4,
"PATCH":1
"MINOR":6,
"PATCH":0
},
"KSP_VERSION_MAX":
{
"MAJOR":1,
"MINOR":4,
"PATCH":1
"MINOR":7,
"PATCH":10
}
}
6 changes: 3 additions & 3 deletions MandatoryRCS-Source/MandatoryRCS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<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"
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\"
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>
<PostBuildEvent>"D:\Projets\KSP\Tools\pdb2mdb\pdb2mdb.exe" "C:\Users\Got\Source\Repos\gotmachine\MandatoryRCS\MandatoryRCS-Source\bin\Debug\MandatoryRCS.dll"
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\"
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>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
104 changes: 64 additions & 40 deletions MandatoryRCS-Source/ModuleTorqueController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
using System.Text;
using UnityEngine;

// TODO :
// - Wheels saturation when landed
// - (Maybe) Torque ON when RCS is ON, to reduce RCS fuel consumption

namespace MandatoryRCS
{
public class ModuleTorqueController : PartModule
Expand Down Expand Up @@ -38,13 +34,6 @@ public class ModuleTorqueController : PartModule

public void Start()
{
// Get the reaction wheel module
rwmodule = part.Modules.GetModule<ModuleReactionWheel>();
// Get the module config torque value
maxTorque.x = rwmodule.PitchTorque;
maxTorque.y = rwmodule.RollTorque;
maxTorque.z = rwmodule.YawTorque;

if (MandatoryRCSSettings.featureReactionWheels)
{
// Does this RW respond to pilot/SAS input ?
Expand Down Expand Up @@ -77,9 +66,16 @@ public void Start()
vessel.OnPreAutopilotUpdate += new FlightInputCallback(GetPilotInput);
callbackIsActive = true;
}
else if (HighLogic.LoadedSceneIsEditor)
{
rwmodule = part.Modules.GetModule<ModuleReactionWheel>();

if (rwmodule == null)
return;

TweakUI(rwmodule);
}
}
// Hide RW control modes and enable/disable toggle from GUI and action groups
TweakUI();
}

private void GetPilotInput(FlightCtrlState st)
Expand All @@ -95,14 +91,45 @@ public void OnDestroy()
vessel.OnPreAutopilotUpdate -= new FlightInputCallback(GetPilotInput);
}
}

// 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.

// 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.
private void UpdateTorque(FlightCtrlState st)
{
// autopilot can be null just before vessel is destroyed
if (vessel.Autopilot == null)
return;

// Not sure why but in some cases the reference the the module can be null if acquired in OnStart.
// To be on the safe side we acquire it here
if (rwmodule == null)
{
// Get the reaction wheel module
rwmodule = part.Modules.GetModule<ModuleReactionWheel>();

if (rwmodule == null)
return;

// Get the module config torque value
maxTorque.x = rwmodule.PitchTorque;
maxTorque.y = rwmodule.RollTorque;
maxTorque.z = rwmodule.YawTorque;

// Hide RW control modes and enable/disable toggle from GUI and action groups
TweakUI(rwmodule);
}

// do this only once, and check for null
// note : not caching it because keeping track of vesselModule changes is a mess
VesselModuleRotation vRotModule = vessel.vesselModules.OfType<VesselModuleRotation>().FirstOrDefault();
if (vRotModule == null)
return;

// We have everything we need, now do the nerf
if (FlightGlobals.ready && vessel.loaded && !vessel.packed)
{
// Get the saturation factor calculated from the vessel rotation
saturationFactor = vessel.vesselModules.OfType<VesselModuleRotation>().First().velSaturationTorqueFactor;
saturationFactor = vRotModule.velSaturationTorqueFactor;

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

if (!isOnTarget && orientationDiff > 0.999f)
{
Expand Down Expand Up @@ -208,37 +235,34 @@ private void SetWheelModuleTorque(Vector3 torque)
// Disable all UI buttons and action groups, except the authority limiter slider.
// RW torque can still be tweaked/disabled trough the renamed for clarity "Reaction Wheel Autority" GUI
// TODO : reenable Normal/SAS/Pilot mode switch if strictMode is disabled
private void TweakUI()
private static void TweakUI(ModuleReactionWheel rw)
{
if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor)
foreach (BaseField f in rw.Fields)
{
foreach (BaseField f in rwmodule.Fields)
if (f.name.Equals("actuatorModeCycle") || f.name.Equals("stateString"))
{
if (f.name.Equals("actuatorModeCycle") || f.name.Equals("stateString"))
{
f.guiActive = false;
f.guiActiveEditor = false;
}
if (f.name.Equals("authorityLimiter"))
{
f.guiName = "Reaction Wheel Authority";
}
// Debug.Log("RW Fields : guiName=" + f.guiName + ", name=" + f.name + ", guiActive=" + f.guiActive + ", guiActiveEditor=" + f.guiActiveEditor);
f.guiActive = false;
f.guiActiveEditor = false;
}
foreach (BaseEvent e in rwmodule.Events)
if (f.name.Equals("authorityLimiter"))
{
if (e.name.Equals("OnToggle"))
{
e.guiActive = false;
e.guiActiveEditor = false;
}
// Debug.Log("RW Events : guiName=" + e.guiName + ", name=" + e.name + ", guiActive=" + e.guiActive + ", guiActiveEditor=" + e.guiActiveEditor);
f.guiName = "Reaction Wheel Authority";
}
foreach (BaseAction a in rwmodule.Actions)
// Debug.Log("RW Fields : guiName=" + f.guiName + ", name=" + f.name + ", guiActive=" + f.guiActive + ", guiActiveEditor=" + f.guiActiveEditor);
}
foreach (BaseEvent e in rw.Events)
{
if (e.name.Equals("OnToggle"))
{
a.active = false;
// Debug.Log("RW Actions : guiName=" + a.guiName + ", name=" + a.name + ", active=" + a.active);
e.guiActive = false;
e.guiActiveEditor = false;
}
// Debug.Log("RW Events : guiName=" + e.guiName + ", name=" + e.name + ", guiActive=" + e.guiActive + ", guiActiveEditor=" + e.guiActiveEditor);
}
foreach (BaseAction a in rw.Actions)
{
a.active = false;
// Debug.Log("RW Actions : guiName=" + a.guiName + ", name=" + a.name + ", active=" + a.active);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions MandatoryRCS-Source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.0.0")]
[assembly: AssemblyFileVersion("1.5.0.0")]
[assembly: AssemblyVersion("1.6.0.0")]
[assembly: AssemblyFileVersion("1.6.0.0")]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ So public domain, feel free to do anything, especially updating this plugin if I
- 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.
- 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.

#### v1.6 for KSP 1.7.0 - 31/05/2019
- Recompiled for KSP 1.7 (should be compatible with KSP 1.6)
- Code tweaks for fail-safe reference acquisition to the stock module. Should fix be the occasional nullref spam that was happening on reentry.

#### v1.5 for KSP 1.4.1
- Recompiled for KSP 1.4.1
- (bugfix) Fixed NRE on asteroids changing SOI
Expand Down

0 comments on commit 3ca4134

Please sign in to comment.