Skip to content

Commit 1dd1ab1

Browse files
authored
Merge pull request #136 from ZiwKerman/develop
Fix for TimeWarp bug
2 parents 67674e1 + 49506f2 commit 1dd1ab1

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

InfernalRobotics/InfernalRobotics/Command/ServoController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,13 @@ private void OnVesselPartCountModified(Vessel v)
293293

294294
private void OnVesselLoaded (Vessel v)
295295
{
296-
Logger.Log("[ServoController] OnVesselLoaded, v=" + v.GetName(), Logger.Level.SuperVerbose);
296+
Logger.Log("[ServoController] OnVesselLoaded, v=" + v.GetName(), Logger.Level.Debug);
297297
RebuildServoGroupsFlight ();
298298
}
299299

300300
private void OnVesselUnloaded (Vessel v)
301301
{
302-
Logger.Log("[ServoController] OnVesselUnloaded, v=" + v.GetName(), Logger.Level.SuperVerbose);
302+
Logger.Log("[ServoController] OnVesselUnloaded, v=" + v.GetName(), Logger.Level.Debug);
303303
RebuildServoGroupsFlight ();
304304
}
305305

InfernalRobotics/InfernalRobotics/Module/ModuleIRServo.cs

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public class ModuleIRServo : PartModule, IRescalable, IJointLockState
5555
[KSPField(isPersistant = false)]
5656
public float jointSpring = 0;
5757
[KSPField(isPersistant = false)]
58-
public float jointDamping = 0;
58+
public float jointDamping = 0;
59+
60+
bool isOnRails = false;
5961

6062
[KSPField(isPersistant = true)] public bool rotateLimits = false;
6163
[KSPField(isPersistant = true)] public float rotateMax = 360;
@@ -131,6 +133,7 @@ public class ModuleIRServo : PartModule, IRescalable, IJointLockState
131133
protected const string ELECTRIC_CHARGE_RESOURCE_NAME = "ElectricCharge";
132134

133135
protected ConfigurableJoint joint;
136+
protected ConfigurableJoint savedJoint;
134137
protected Rigidbody jointRigidBody;
135138

136139
protected SoundSource motorSound;
@@ -427,9 +430,62 @@ public override void OnAwake()
427430

428431
Translator.Init(isMotionLock, new Servo(this), Interpolator);
429432

433+
GameEvents.onVesselGoOnRails.Add (OnVesselGoOnRails);
434+
GameEvents.onVesselGoOffRails.Add (OnVesselGoOffRails);
435+
430436
Logger.Log(string.Format("[OnAwake] End, rotateLimits={0}, minTweak={1}, maxTweak={2}, rotateJoint={0}", rotateLimits, minTweak, maxTweak), Logger.Level.Debug);
431437
}
432-
438+
439+
public void OnVesselGoOnRails (Vessel v)
440+
{
441+
if (v != vessel)
442+
return;
443+
444+
Logger.Log ("[OnVesselGoOnRails] Reverting Joint", Logger.Level.Debug);
445+
446+
part.attachJoint.Joint.angularXDrive = savedJoint.angularXDrive;
447+
part.attachJoint.Joint.angularYZDrive = savedJoint.angularYZDrive;
448+
part.attachJoint.Joint.xDrive = savedJoint.xDrive;
449+
part.attachJoint.Joint.yDrive = savedJoint.yDrive;
450+
part.attachJoint.Joint.zDrive = savedJoint.zDrive;
451+
part.attachJoint.Joint.enableCollision = false;
452+
453+
if (joint)
454+
{
455+
// lock all movement by default
456+
joint.xMotion = ConfigurableJointMotion.Locked;
457+
joint.yMotion = ConfigurableJointMotion.Locked;
458+
joint.zMotion = ConfigurableJointMotion.Locked;
459+
joint.angularXMotion = ConfigurableJointMotion.Locked;
460+
joint.angularYMotion = ConfigurableJointMotion.Locked;
461+
joint.angularZMotion = ConfigurableJointMotion.Locked;
462+
}
463+
464+
rotationDelta = GetRealRotation();
465+
translationDelta = GetRealTranslation();
466+
467+
isOnRails = true;
468+
}
469+
470+
471+
public void OnVesselGoOffRails (Vessel v)
472+
{
473+
if (v != vessel)
474+
return;
475+
476+
JointSetupDone = false;
477+
Logger.Log ("[OnVesselGoOffRails] Resetting Joint", Logger.Level.Debug);
478+
479+
if (joint)
480+
{
481+
DestroyImmediate (joint);
482+
}
483+
484+
SetupJoints ();
485+
486+
isOnRails = false;
487+
}
488+
433489
public override void OnSave(ConfigNode node)
434490
{
435491
Logger.Log("[OnSave] Start", Logger.Level.Debug);
@@ -563,15 +619,17 @@ protected virtual void AttachToParent()
563619
{
564620
Transform fix = FixedMeshTransform;
565621
//Transform fix = obj;
622+
623+
566624
if (rotateJoint)
567625
{
568-
fix.RotateAround(transform.TransformPoint(rotatePivot), transform.TransformDirection(rotateAxis),
626+
fix.RotateAround(part.transform.TransformPoint(rotatePivot), part.transform.TransformDirection(rotateAxis),
569627
//(invertSymmetry ? ((part.symmetryCounterparts.Count != 1) ? -1 : 1) : -1) *
570628
-rotation);
571629
}
572630
else if (translateJoint)
573631
{
574-
fix.Translate(transform.TransformDirection(translateAxis.normalized)*translation, Space.World);
632+
fix.Translate(part.transform.TransformDirection(translateAxis.normalized)*translation, Space.World);
575633
}
576634
fix.parent = part.parent.transform;
577635
}
@@ -746,19 +804,11 @@ public virtual bool SetupJoints()
746804
return false;
747805
}
748806

749-
if (part.attachJoint.Joint.xDrive.maximumForce > 0.0001)
750-
{
751-
JointSetupDone = false;
752-
Logger.Log ("Resetting Joint", Logger.Level.Debug);
753-
if(joint)
754-
{
755-
DestroyImmediate (joint);
756-
}
757-
}
758-
759807
if (JointSetupDone)
760808
return false;
761-
809+
810+
savedJoint = part.attachJoint.Joint;
811+
762812
// Catch reversed joint
763813
// Maybe there is a best way to do it?
764814
if (transform.position != part.attachJoint.Joint.connectedBody.transform.position)
@@ -1387,6 +1437,9 @@ public void FixedUpdate()
13871437
return;
13881438
}
13891439

1440+
if (isOnRails)
1441+
return;
1442+
13901443
if (minTweak > maxTweak)
13911444
{
13921445
maxTweak = minTweak;

0 commit comments

Comments
 (0)