From abd2578127815e4dd97cce7019d4b209e57a9227 Mon Sep 17 00:00:00 2001 From: Brett Ryland Date: Sun, 31 Dec 2023 01:01:52 +0100 Subject: [PATCH 1/3] Fix various NREs (and hopefully the SIGSEGV). --- BDArmory/Control/MissileFire.cs | 10 +++++----- BDArmory/Radar/VesselRadarData.cs | 4 +++- BDArmory/Weapons/Missiles/MissileBase.cs | 11 +++++++---- BDArmory/Weapons/Missiles/MissileLauncher.cs | 10 ++++++---- BDArmory/Weapons/Missiles/MultiMissileLauncher.cs | 15 +++++++++------ TODO.md | 3 ++- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/BDArmory/Control/MissileFire.cs b/BDArmory/Control/MissileFire.cs index abfa50ed8..75b2bad04 100644 --- a/BDArmory/Control/MissileFire.cs +++ b/BDArmory/Control/MissileFire.cs @@ -2055,14 +2055,14 @@ IEnumerator GuardMissileRoutine(Vessel targetVessel, MissileBase ml) } else { - if (BDArmorySettings.DEBUG_MISSILES) Debug.Log($"[BDArmory.MissileFire]: {vessel.vesselName}'s {CurrentMissile.name} could not lock, attempting unguided fire."); + if (BDArmorySettings.DEBUG_MISSILES) Debug.Log($"[BDArmory.MissileFire]: {vessel.vesselName}'s {(CurrentMissile ? CurrentMissile.name : "null missile")} could not lock, attempting unguided fire."); unguidedWeapon = true; //so let them be used as unguided ordinance } } } else //no radar, missiles now expensive unguided ordinance { - if (BDArmorySettings.DEBUG_MISSILES) Debug.Log($"[BDArmory.MissileFire]: {vessel.vesselName}'s {CurrentMissile.name} has no radar, attempting unguided fire."); + if (BDArmorySettings.DEBUG_MISSILES) Debug.Log($"[BDArmory.MissileFire]: {vessel.vesselName}'s {(CurrentMissile ? CurrentMissile.name : "null missile")} has no radar, attempting unguided fire."); unguidedWeapon = true; //so let them be used as unguided ordinance } break; @@ -2475,7 +2475,7 @@ IEnumerator GuardMissileRoutine(Vessel targetVessel, MissileBase ml) MissileLauncher mlauncher = ml as MissileLauncher; if (mlauncher && mlauncher.multiLauncher && mlauncher.multiLauncher.overrideReferenceTransform) { - if (BDArmorySettings.DEBUG_MISSILES) Debug.Log($"[BDArmory.MissileFire]: {vessel.vesselName}'s {CurrentMissile.name} launched from MML, aborting unguided launch."); + if (BDArmorySettings.DEBUG_MISSILES) Debug.Log($"[BDArmory.MissileFire]: {vessel.vesselName}'s {(CurrentMissile ? CurrentMissile.name : "null missile")} launched from MML, aborting unguided launch."); } else { @@ -6855,10 +6855,10 @@ public void SendTargetDataToMissile(MissileBase ml, bool clearHeat = true) if (BDArmorySettings.DEBUG_MISSILES) { Debug.Log($"[BDArmory.MissileData]: Sending targetInfo to {(dumbfire ? "dumbfire" : "")}{Enum.GetName(typeof(MissileBase.TargetingModes), ml.TargetingMode)} Missile..."); - if (ml.targetVessel != null) Debug.Log($"[BDArmory.MissileData]: targetInfo sent for {ml.targetVessel.Vessel.GetName()}"); + if (ml.targetVessel != null) Debug.Log($"[BDArmory.MissileData]: targetInfo sent for {(ml.targetVessel.Vessel != null ? ml.targetVessel.Vessel.GetName() : "null target")}"); } if (BDArmorySettings.DEBUG_MISSILES) - Debug.Log($"[BDArmory.MissileData]: firing missile at {(currentTarget != null ? currentTarget.Vessel.GetName() : "null target")}"); + Debug.Log($"[BDArmory.MissileData]: firing missile at {(currentTarget != null && currentTarget.Vessel != null ? currentTarget.Vessel.GetName() : "null target")}"); } #endregion Targeting diff --git a/BDArmory/Radar/VesselRadarData.cs b/BDArmory/Radar/VesselRadarData.cs index fb7a1e08a..001001347 100644 --- a/BDArmory/Radar/VesselRadarData.cs +++ b/BDArmory/Radar/VesselRadarData.cs @@ -212,7 +212,7 @@ public TargetSignatureData activeIRTarget(Vessel desiredTarget, MissileFire mf) else { if (displayedTargets[i].targetData.Team == mf.Team) continue; - if (displayedTargets[i].targetData.signalStrength > targetMagnitude ) + if (displayedTargets[i].targetData.signalStrength > targetMagnitude) { targetMagnitude = displayedTargets[i].targetData.signalStrength; brightestTarget = i; @@ -1023,6 +1023,7 @@ private void WindowRadar(int windowID) for (int i = 0; i < rCount; i++) { + if (availableRadars[i] == null || availableRadars[i].gameObject == null) continue; bool canScan = availableRadars[i].canScan; bool canTrackWhileScan = availableRadars[i].canTrackWhileScan; bool islocked = availableRadars[i].locked; @@ -1076,6 +1077,7 @@ private void WindowRadar(int windowID) } for (int i = 0; i < iCount; i++) { + if (availableIRSTs[i] == null || availableIRSTs[i].gameObject == null) continue; bool canScan = availableIRSTs[i].canScan; float currentAngle = availableIRSTs[i].currentAngle; diff --git a/BDArmory/Weapons/Missiles/MissileBase.cs b/BDArmory/Weapons/Missiles/MissileBase.cs index 9e9aa80a7..71ecb55d1 100644 --- a/BDArmory/Weapons/Missiles/MissileBase.cs +++ b/BDArmory/Weapons/Missiles/MissileBase.cs @@ -404,7 +404,11 @@ public float Throttle public TargetInfo targetVessel { - get { return _targetVessel; } + get + { + if (_targetVessel != null && _targetVessel.Vessel == null) _targetVessel = null; + return _targetVessel; + } set { _targetVessel = value; @@ -818,10 +822,9 @@ protected void UpdateRadarTarget() { TargetAcquired = false; - float angleToTarget = Vector3.Angle(radarTarget.predictedPosition - transform.position, GetForwardTransform()); - if (radarTarget.exists) { + float angleToTarget = Vector3.Angle(radarTarget.predictedPosition - transform.position, GetForwardTransform()); // locked-on before launch, passive radar guidance or waiting till in active radar range: if (!ActiveRadar && ((radarTarget.predictedPosition - transform.position).sqrMagnitude > (activeRadarRange * activeRadarRange) || angleToTarget > maxOffBoresight * 0.75f)) { @@ -1249,7 +1252,7 @@ public Vector3d UpdateInertialTarget() } else //clamp updates to radar/IRST track speed { - float updateCount = TimeIndex / GpsUpdateMax; + float updateCount = TimeIndex / GpsUpdateMax; if (updateCount > gpsUpdateCounter) { gpsUpdateCounter++; diff --git a/BDArmory/Weapons/Missiles/MissileLauncher.cs b/BDArmory/Weapons/Missiles/MissileLauncher.cs index bc3efc6d0..6d803ec47 100644 --- a/BDArmory/Weapons/Missiles/MissileLauncher.cs +++ b/BDArmory/Weapons/Missiles/MissileLauncher.cs @@ -1345,11 +1345,13 @@ public void MissileLaunch() } catch (Exception e) { - Debug.LogError("[BDArmory.MissileLauncher]: DEBUG " + e.Message); + Debug.LogError("[BDArmory.MissileLauncher]: DEBUG " + e.Message + "\n" + e.StackTrace); try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null part?: " + (part == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG part: " + e2.Message); } try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null part.rb?: " + (part.rb == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG part.rb: " + e2.Message); } try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null BDATargetManager.FiredMissiles?: " + (BDATargetManager.FiredMissiles == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG BDATargetManager.FiredMissiles: " + e2.Message); } try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null vessel?: " + (vessel == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG vessel: " + e2.Message); } + try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null targetVessel?: " + (targetVessel == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG targetVessel: " + e2.Message); } + try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null targetVessel.Vessel?: " + (targetVessel.Vessel == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG targetVessel.Vessel: " + e2.Message); } try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null sfAudioSource?: " + (sfAudioSource == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG sfAudioSource: " + e2.Message); } throw; // Re-throw the exception so behaviour is unchanged so we see it. } @@ -2128,7 +2130,7 @@ void StartBoost() if (string.IsNullOrEmpty(boostTransformName)) { boostEmitters = pEmitters; - if (hasRCS && rcsTransforms!=null) boostEmitters.RemoveAll(pe => rcsTransforms.Contains(pe)); + if (hasRCS && rcsTransforms != null) boostEmitters.RemoveAll(pe => rcsTransforms.Contains(pe)); if (hasRCS && forwardRCS && !boostEmitters.Contains(forwardRCS)) boostEmitters.Add(forwardRCS); boostGaplessEmitters = gaplessEmitters; } @@ -2790,8 +2792,8 @@ void WarnTarget() void SetupRCS() { - rcsFiredTimes = [ 0, 0, 0, 0 ]; - rcsTransforms = [ upRCS, leftRCS, rightRCS, downRCS ]; + rcsFiredTimes = [0, 0, 0, 0]; + rcsTransforms = [upRCS, leftRCS, rightRCS, downRCS]; } void DoRCS() diff --git a/BDArmory/Weapons/Missiles/MultiMissileLauncher.cs b/BDArmory/Weapons/Missiles/MultiMissileLauncher.cs index 799e78522..cf8ce3688 100644 --- a/BDArmory/Weapons/Missiles/MultiMissileLauncher.cs +++ b/BDArmory/Weapons/Missiles/MultiMissileLauncher.cs @@ -1055,14 +1055,17 @@ IEnumerator salvoFire(bool LaunchThenDestroy) if (deployState != null) { yield return new WaitForSecondsFixed(0.5f); //wait for missile to clear bay - deployState.enabled = true; - deployState.speed = -deploySpeed / deployState.length; - yield return new WaitWhileFixed(() => deployState != null && deployState.normalizedTime > 0); if (deployState != null) { - deployState.normalizedTime = 0; - deployState.speed = 0; - deployState.enabled = false; + deployState.enabled = true; + deployState.speed = -deploySpeed / deployState.length; + yield return new WaitWhileFixed(() => deployState != null && deployState.normalizedTime > 0); + if (deployState != null) + { + deployState.normalizedTime = 0; + deployState.speed = 0; + deployState.enabled = false; + } } } if (missileLauncher == null) yield break; diff --git a/TODO.md b/TODO.md index a635b6d10..add238d6f 100644 --- a/TODO.md +++ b/TODO.md @@ -12,11 +12,12 @@ - Add tooltips to settings. - Add thumbnails to the craft browser. +- Figure out how to add a 0 value to UI_FloatSemiLogRange sliders. +- Stability of the targeting cam when slewing with the mouse (jitter). - Wiki entries - Auto-Tuning -- Stability of the targeting cam when slewing with the mouse. - Requests from discord: - ? Add an action group trigger to the WM based on the current target being an enemy vessel within a custom distance. - Make it a collapsable section of custom triggers to include other conditions later. - Artillery aiming support From e3b1d36c16208b18531a41638c1084d20de9b751 Mon Sep 17 00:00:00 2001 From: Brett Ryland Date: Sun, 31 Dec 2023 01:09:16 +0100 Subject: [PATCH 2/3] Remove some now unneeded checks. --- BDArmory/Control/MissileFire.cs | 4 ++-- BDArmory/UI/LoadedVesselSwitcher.cs | 2 +- BDArmory/Weapons/Missiles/MissileBase.cs | 2 +- BDArmory/Weapons/Missiles/MissileLauncher.cs | 1 - 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/BDArmory/Control/MissileFire.cs b/BDArmory/Control/MissileFire.cs index 75b2bad04..a81f6e882 100644 --- a/BDArmory/Control/MissileFire.cs +++ b/BDArmory/Control/MissileFire.cs @@ -6435,7 +6435,7 @@ public void SetTarget(TargetInfo target) } else { - if (PreviousMissile != null && PreviousMissile.ActiveRadar && PreviousMissile.targetVessel != null && PreviousMissile.targetVessel.Vessel != null) //previous missile has gone active, don't need that lock anymore + if (PreviousMissile != null && PreviousMissile.ActiveRadar && PreviousMissile.targetVessel != null) //previous missile has gone active, don't need that lock anymore { vesselRadarData.UnlockSelectedTarget(PreviousMissile.targetVessel.Vessel); } @@ -6855,7 +6855,7 @@ public void SendTargetDataToMissile(MissileBase ml, bool clearHeat = true) if (BDArmorySettings.DEBUG_MISSILES) { Debug.Log($"[BDArmory.MissileData]: Sending targetInfo to {(dumbfire ? "dumbfire" : "")}{Enum.GetName(typeof(MissileBase.TargetingModes), ml.TargetingMode)} Missile..."); - if (ml.targetVessel != null) Debug.Log($"[BDArmory.MissileData]: targetInfo sent for {(ml.targetVessel.Vessel != null ? ml.targetVessel.Vessel.GetName() : "null target")}"); + if (ml.targetVessel != null) Debug.Log($"[BDArmory.MissileData]: targetInfo sent for {ml.targetVessel.Vessel.GetName()}"); } if (BDArmorySettings.DEBUG_MISSILES) Debug.Log($"[BDArmory.MissileData]: firing missile at {(currentTarget != null && currentTarget.Vessel != null ? currentTarget.Vessel.GetName() : "null target")}"); diff --git a/BDArmory/UI/LoadedVesselSwitcher.cs b/BDArmory/UI/LoadedVesselSwitcher.cs index 079726d9f..a88f7fe0a 100644 --- a/BDArmory/UI/LoadedVesselSwitcher.cs +++ b/BDArmory/UI/LoadedVesselSwitcher.cs @@ -1168,7 +1168,7 @@ private void UpdateCamera() var targetDirection = missile.TargetPosition - missile.transform.position; var targetDistance = targetDirection.magnitude; if (Vector3.Dot(targetDirection, missile.GetForwardTransform()) < 0.5f * targetDistance) continue; // Ignore off-target missiles. - if (missile.targetVessel != null && missile.targetVessel.Vessel != null && missile.targetVessel.Vessel.IsMissile()) continue; // Ignore missiles targeting missiles. + if (missile.targetVessel != null && missile.targetVessel.Vessel.IsMissile()) continue; // Ignore missiles targeting missiles. if (Vector3.Dot(missile.TargetVelocity - missile.vessel.Velocity(), missile.GetForwardTransform()) > -1f) continue; // Ignore missiles that aren't gaining on their targets. float missileScore = targetDistance < 1e3f ? 0.1f : 0.1f + (targetDistance - 1e3f) * (targetDistance - 1e3f) * 5e-8f; // Prioritise missiles that are within 1km from their targets and de-prioritise those more than 5km away. if (missileScore < bestScore) diff --git a/BDArmory/Weapons/Missiles/MissileBase.cs b/BDArmory/Weapons/Missiles/MissileBase.cs index 71ecb55d1..acef68723 100644 --- a/BDArmory/Weapons/Missiles/MissileBase.cs +++ b/BDArmory/Weapons/Missiles/MissileBase.cs @@ -406,7 +406,7 @@ public TargetInfo targetVessel { get { - if (_targetVessel != null && _targetVessel.Vessel == null) _targetVessel = null; + if (_targetVessel != null && _targetVessel.Vessel == null) _targetVessel = null; // The vessel could die before _targetVessel gets cleared otherwise. return _targetVessel; } set diff --git a/BDArmory/Weapons/Missiles/MissileLauncher.cs b/BDArmory/Weapons/Missiles/MissileLauncher.cs index 6d803ec47..0f9d74b99 100644 --- a/BDArmory/Weapons/Missiles/MissileLauncher.cs +++ b/BDArmory/Weapons/Missiles/MissileLauncher.cs @@ -1351,7 +1351,6 @@ public void MissileLaunch() try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null BDATargetManager.FiredMissiles?: " + (BDATargetManager.FiredMissiles == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG BDATargetManager.FiredMissiles: " + e2.Message); } try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null vessel?: " + (vessel == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG vessel: " + e2.Message); } try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null targetVessel?: " + (targetVessel == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG targetVessel: " + e2.Message); } - try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null targetVessel.Vessel?: " + (targetVessel.Vessel == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG targetVessel.Vessel: " + e2.Message); } try { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG null sfAudioSource?: " + (sfAudioSource == null)); } catch (Exception e2) { Debug.LogWarning("[BDArmory.MissileLauncher]: DEBUG sfAudioSource: " + e2.Message); } throw; // Re-throw the exception so behaviour is unchanged so we see it. } From 9052aab31e567ab447d4a8ccb8a3231571be02e0 Mon Sep 17 00:00:00 2001 From: Brett Ryland Date: Sun, 31 Dec 2023 13:16:14 +0100 Subject: [PATCH 3/3] Add a null check for the current missile when displaying radar contacts. --- BDArmory/Radar/VesselRadarData.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BDArmory/Radar/VesselRadarData.cs b/BDArmory/Radar/VesselRadarData.cs index 001001347..dc2907ac0 100644 --- a/BDArmory/Radar/VesselRadarData.cs +++ b/BDArmory/Radar/VesselRadarData.cs @@ -2066,7 +2066,7 @@ private void DrawDisplayedContacts() if (weaponManager.selectedWeapon.GetWeaponClass() == WeaponClasses.Missile || weaponManager.selectedWeapon.GetWeaponClass() == WeaponClasses.SLW) { MissileBase currMissile = weaponManager.CurrentMissile; - if (currMissile.TargetingMode == MissileBase.TargetingModes.Radar || currMissile.TargetingMode == MissileBase.TargetingModes.Heat) + if (currMissile && (currMissile.TargetingMode == MissileBase.TargetingModes.Radar || currMissile.TargetingMode == MissileBase.TargetingModes.Heat)) { MissileLaunchParams dlz = MissileLaunchParams.GetDynamicLaunchParams(currMissile, lockedTarget.velocity, lockedTarget.predictedPosition); float rangeToPixels = (1 / rIncrements[rangeIndex]) * RadarDisplayRect.height;