Skip to content

Commit

Permalink
Handle VM instantiation without load
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanKell committed Nov 5, 2023
1 parent b86ff1c commit f02cdc4
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions Source/VesselModuleRotationRO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class VesselModuleRotationRO : VesselModule
private int _retrySASCount;
private int _setSASMode;

private bool _isInitialized = false;



private const float RotationThreshold = 0.1f * Mathf.Deg2Rad;
Expand Down Expand Up @@ -142,8 +144,21 @@ private void SetPosRot(Quaternion rotation, Vector3d position)

public static bool IgnoreRot(Vessel vessel) => vessel.situation == Vessel.Situations.PRELAUNCH || vessel.situation == Vessel.Situations.LANDED || vessel.situation == Vessel.Situations.SPLASHED;

private void Initialize()
{
_isInitialized = true;
StoreRot();
referenceUpLocal = vessel.transform.InverseTransformDirection(vessel.GetTransform().up);
loadedVersion = VERSION;
_restoreSAS = false;
_restoreTarget = false;
_restoreManeuverHash = false;
_restoreAngularVelocity = false;
}

protected override void OnLoad(ConfigNode node)
{
_isInitialized = true;
_restoreTarget = true;
_restoreManeuverHash = true;

Expand All @@ -166,6 +181,17 @@ protected override void OnLoad(ConfigNode node)
SetRot();
}

protected override void OnAwake()
{
// will be stomped in OnLoad if this is via AddComponent
if (loadedVersion < VERSION)
{
StoreRot();
referenceUpLocal = vessel.transform.InverseTransformDirection(vessel.GetTransform().up);
loadedVersion = VERSION;
}
}

public void RestoreState()
{
if (_restoreManeuverHash)
Expand Down Expand Up @@ -228,20 +254,21 @@ private void FixedUpdate()
if (!IsEnabled)
return;

if (!_isInitialized)
Initialize();

RestoreState();

//bool packRotate = false;
if (vessel.loaded)
{
// packed:
// Vessel is loaded but not in physics, either because
// - It is in the physics bubble but in non-physics timewarp
// - It has gone outside of the physics bubble
// - It was just loaded, is in the physics bubble and will be unpacked in a few frames
if (vessel.packed)
{
//packRotate = true;
}
else if (FlightGlobals.ready) // The vessel is in physics simulation and fully loaded

// unpacked:
if (!vessel.packed && FlightGlobals.ready) // The vessel is in physics simulation and fully loaded
{
bool okToSaveAngularVelocity = true;

Expand All @@ -257,23 +284,16 @@ private void FixedUpdate()
SaveOffRailsStatus(okToSaveAngularVelocity);
}
}
//else
//{
// packRotate = true;

//}

//if (packRotate)
//{
// RailsUpdate(vessel.vesselTransform.position);
//}

// Saving this FixedUpdate target, autopilot context and maneuver node, to check if they have changed in the next FixedUpdate
SaveLastUpdateStatus();
}

public void RailsUpdate(Vector3d pos)
{
if (!_isInitialized)
Initialize();

// Check if target / maneuver is modified/deleted during timewarp
bool holdValid = false;
if (autopilotTargetHold)
Expand Down

0 comments on commit f02cdc4

Please sign in to comment.