From b97d9bbe9ed2b27f31cdef119a5fc32632211152 Mon Sep 17 00:00:00 2001
From: TheKurgan <dunewf@hotmail.com>
Date: Fri, 3 Apr 2020 18:44:36 -0400
Subject: [PATCH] Reverted some of the changes to commit on Oct 27 2019 in
 order to fix radar guided missiles not losing lock

---
 BDArmory/Modules/BDModularGuidance.cs | 64 +++++++++---------
 BDArmory/Modules/MissileBase.cs       | 35 ----------
 BDArmory/Modules/MissileLauncher.cs   | 93 ++++++++++++++-------------
 3 files changed, 80 insertions(+), 112 deletions(-)

diff --git a/BDArmory/Modules/BDModularGuidance.cs b/BDArmory/Modules/BDModularGuidance.cs
index 15d6e830f..d9c0cd22c 100644
--- a/BDArmory/Modules/BDModularGuidance.cs
+++ b/BDArmory/Modules/BDModularGuidance.cs
@@ -617,40 +617,40 @@ private Vector3 CruiseGuidance()
             return this._guidance.GetDirection(this,TargetPosition);
         }
 
-        //private void CheckMiss(Vector3 targetPosition)
-        //{
-        //    if (HasMissed) return;
-        //    // if I'm to close to my vessel avoid explosion
-        //    if ((vessel.CoM - SourceVessel.CoM).magnitude < 4 * DetonationDistance) return;
-        //    // if I'm getting closer to  my target avoid explosion
-        //    if ((vessel.CoM - targetPosition).sqrMagnitude >
-        //        (vessel.CoM + (vessel.Velocity() * Time.fixedDeltaTime) - (targetPosition + (TargetVelocity * Time.fixedDeltaTime))).sqrMagnitude) return;
-
-        //    if (MissileState != MissileStates.PostThrust ) return;
-
-        //    Debug.Log("[BDArmory]: Missile CheckMiss showed miss");
-        //    HasMissed = true;
-        //    guidanceActive = false;
-        //    TargetMf = null;
-        //    isTimed = true;
-        //    detonationTime = TimeIndex + 1.5f;
-        //}
-
-        //private void CheckMiss()
-        //{
-        //    if (HasMissed) return;
+        private void CheckMiss(Vector3 targetPosition)
+        {
+            if (HasMissed) return;
+            // if I'm to close to my vessel avoid explosion
+            if ((vessel.CoM - SourceVessel.CoM).magnitude < 4 * DetonationDistance) return;
+            // if I'm getting closer to  my target avoid explosion
+            if ((vessel.CoM - targetPosition).sqrMagnitude >
+                (vessel.CoM + (vessel.Velocity() * Time.fixedDeltaTime) - (targetPosition + (TargetVelocity * Time.fixedDeltaTime))).sqrMagnitude) return;
+
+            if (MissileState != MissileStates.PostThrust ) return;
+
+            Debug.Log("[BDArmory]: Missile CheckMiss showed miss");
+            HasMissed = true;
+            guidanceActive = false;
+            TargetMf = null;
+            isTimed = true;
+            detonationTime = TimeIndex + 1.5f;
+        }
+
+        private void CheckMiss()
+        {
+            if (HasMissed) return;
         
 
-        //    if (MissileState == MissileStates.PostThrust && (vessel.LandedOrSplashed || vessel.Velocity().magnitude < 10f))
-        //    {
-        //        Debug.Log("[BDArmory]: Missile CheckMiss showed miss");
-        //        HasMissed = true;
-        //        guidanceActive = false;
-        //        TargetMf = null;
-        //        isTimed = true;
-        //        detonationTime = TimeIndex + 1.5f;
-        //    }
-        //}
+            if (MissileState == MissileStates.PostThrust && (vessel.LandedOrSplashed || vessel.Velocity().magnitude < 10f))
+            {
+                Debug.Log("[BDArmory]: Missile CheckMiss showed miss");
+                HasMissed = true;
+                guidanceActive = false;
+                TargetMf = null;
+                isTimed = true;
+                detonationTime = TimeIndex + 1.5f;
+            }
+        }
 
 
         public void GuidanceSteer(FlightCtrlState s)
diff --git a/BDArmory/Modules/MissileBase.cs b/BDArmory/Modules/MissileBase.cs
index 2bc1acdf1..0160d4098 100644
--- a/BDArmory/Modules/MissileBase.cs
+++ b/BDArmory/Modules/MissileBase.cs
@@ -1013,41 +1013,6 @@ public void CheckDetonationState()
             }
         }
 
-        public void CheckMiss(Vector3 targetPosition)
-        {
-            if (HasMissed) return;
-            // if I'm to close to my vessel avoid explosion
-            if ((vessel.CoM - SourceVessel.CoM).magnitude < 4 * DetonationDistance) return;
-            // if I'm getting closer to  my target avoid explosion
-            if ((vessel.CoM - targetPosition).sqrMagnitude >
-                (vessel.CoM + (vessel.Velocity() * Time.fixedDeltaTime) - (targetPosition + (TargetVelocity * Time.fixedDeltaTime))).sqrMagnitude) return;
-
-            if (MissileState != MissileStates.PostThrust) return;
-
-            Debug.Log("[BDArmory]: Missile CheckMiss showed miss");
-            HasMissed = true;
-            guidanceActive = false;
-            TargetMf = null;
-            isTimed = true;
-            detonationTime = TimeIndex + 1.5f;
-        }
-
-        public void CheckMiss()
-        {
-            if (HasMissed) return;
-
-
-            if (MissileState == MissileStates.PostThrust && (vessel.LandedOrSplashed || vessel.Velocity().magnitude < 10f))
-            {
-                Debug.Log("[BDArmory]: Missile CheckMiss showed miss");
-                HasMissed = true;
-                guidanceActive = false;
-                TargetMf = null;
-                isTimed = true;
-                detonationTime = TimeIndex + 1.5f;
-            }
-        }
-
         protected void SetInitialDetonationDistance()
         {
             if (this.DetonationDistance == -1)
diff --git a/BDArmory/Modules/MissileLauncher.cs b/BDArmory/Modules/MissileLauncher.cs
index 2550a5a42..33bd86a55 100644
--- a/BDArmory/Modules/MissileLauncher.cs
+++ b/BDArmory/Modules/MissileLauncher.cs
@@ -872,48 +872,48 @@ public override void OnFixedUpdate()
             }
         }
 
-        //private void CheckMiss()
-        //{
-        //    float sqrDist = (float) ((TargetPosition + (TargetVelocity * Time.fixedDeltaTime)) - (vessel.CoM + (vessel.Velocity() * Time.fixedDeltaTime))).sqrMagnitude;
-        //    if (sqrDist < 160000 || MissileState == MissileStates.PostThrust)
-        //    {
-        //        checkMiss = true;
-        //    }
-        //    if (maxAltitude != 0f)
-        //    {
-        //        if (vessel.altitude >= maxAltitude) checkMiss = true;
-        //    }
-
-        //    //kill guidance if missileBase has missed
-        //    if (!HasMissed && checkMiss)
-        //    {
-        //        bool noProgress = MissileState == MissileStates.PostThrust && (Vector3.Dot(vessel.Velocity() - TargetVelocity, TargetPosition - vessel.transform.position) < 0);
-        //        if (Vector3.Dot(TargetPosition - transform.position, transform.forward) < 0 || noProgress)
-        //        {
-        //            Debug.Log("[BDArmory]: Missile has missed!");
-
-        //            if (vessel.altitude >= maxAltitude && maxAltitude != 0f)
-        //                Debug.Log("[BDArmory]: CheckMiss trigged by MaxAltitude");
-
-        //            HasMissed = true;
-        //            guidanceActive = false;
-
-        //            TargetMf = null;
-
-        //            MissileLauncher launcher = this as MissileLauncher;
-        //            if (launcher != null)
-        //            {
-        //                if (launcher.hasRCS) launcher.KillRCS();
-        //            }
-
-        //            if (sqrDist < Mathf.Pow(GetBlastRadius() * 0.5f, 2)) part.Destroy();
-
-        //            isTimed = true;
-        //            detonationTime = TimeIndex + 1.5f;
-        //            return;
-        //        }
-        //    }
-        //}
+        private void CheckMiss()
+        {
+            float sqrDist = (float) ((TargetPosition + (TargetVelocity * Time.fixedDeltaTime)) - (vessel.CoM + (vessel.Velocity() * Time.fixedDeltaTime))).sqrMagnitude;
+            if (sqrDist < 160000 || MissileState == MissileStates.PostThrust)
+            {
+                checkMiss = true;
+            }
+            if (maxAltitude != 0f)
+            {
+                if (vessel.altitude >= maxAltitude) checkMiss = true;
+            }
+
+            //kill guidance if missileBase has missed
+            if (!HasMissed && checkMiss)
+            {
+                bool noProgress = MissileState == MissileStates.PostThrust && (Vector3.Dot(vessel.Velocity() - TargetVelocity, TargetPosition - vessel.transform.position) < 0);
+                if (Vector3.Dot(TargetPosition - transform.position, transform.forward) < 0 || noProgress)
+                {
+                    Debug.Log("[BDArmory]: Missile has missed!");
+
+                    if (vessel.altitude >= maxAltitude && maxAltitude != 0f)
+                        Debug.Log("[BDArmory]: CheckMiss trigged by MaxAltitude");
+
+                    HasMissed = true;
+                    guidanceActive = false;
+
+                    TargetMf = null;
+
+                    MissileLauncher launcher = this as MissileLauncher;
+                    if (launcher != null)
+                    {
+                        if (launcher.hasRCS) launcher.KillRCS();
+                    }
+
+                    if (sqrDist < Mathf.Pow(GetBlastRadius() * 0.5f, 2)) part.Destroy();
+
+                    isTimed = true;
+                    detonationTime = TimeIndex + 1.5f;
+                    return;
+                }
+            }
+        }
 
 
         void UpdateGuidance()
@@ -1046,10 +1046,10 @@ void UpdateGuidance()
                         SLWGuidance();
                     }
 
-                    CheckMiss(TargetPosition);
                 }
                 else
                 {
+                    CheckMiss();
                     TargetMf = null;
                     if (aero)
                     {
@@ -1067,8 +1067,6 @@ void UpdateGuidance()
                     KillRCS();
                 }
             }
-
-            CheckMiss();
         }
 
         // feature_engagementenvelope: terminal guidance mode for cruise missiles
@@ -1602,6 +1600,7 @@ void CruiseGuidance()
                 vesselReferenceTransform.rotation = Quaternion.LookRotation(-rotationTransform.up, rotationTransform.forward);
             }
             DoAero(cruiseTarget);
+            CheckMiss();
         }
 
         void AAMGuidance()
@@ -1632,6 +1631,7 @@ void AAMGuidance()
             if (TimeIndex > dropTime + 0.25f)
             {
                 DoAero(aamTarget);
+                CheckMiss();
             }
 
         }
@@ -1650,6 +1650,7 @@ void AGMGuidance()
                         Debug.Log("[BDArmory]: AGM Missile guidance failed - target out of view");
                         guidanceActive = false;
                     }
+                    CheckMiss();
                 }
                 else
                 {
@@ -1697,6 +1698,8 @@ void SLWGuidance()
 
             if (SLWTarget.y > 0f) SLWTarget.y = getSWLWOffset;
 
+            CheckMiss();
+
         }
 
         void DoAero(Vector3 targetPosition)