From 96244ef3fd072b2c780d11a1e3a9d4d12dc5e48f Mon Sep 17 00:00:00 2001
From: JonnyOThan <jonnyothan@gmail.com>
Date: Sat, 9 Nov 2024 16:20:03 -0500
Subject: [PATCH] Fix #377: fix fatal IVA issue triggered by KPBS -MAS was not
 properly handling parts that have ModuleWheelDeployment and not
 ModuleWheelDamage

---
 AvionicsSystems.csproj             | 10 ++++++++++
 Source/MASVesselComputerModules.cs |  7 +++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/AvionicsSystems.csproj b/AvionicsSystems.csproj
index 7a143f9d..a5c09ee0 100644
--- a/AvionicsSystems.csproj
+++ b/AvionicsSystems.csproj
@@ -150,6 +150,16 @@
   <ItemGroup>
     <None Include="packages.config" />
   </ItemGroup>
+  <ItemGroup>
+    <Publicize Include="Assembly-CSharp" />
+  </ItemGroup>
+  <ItemGroup>
+    <PackageReference Include="Krafs.Publicizer">
+      <Version>2.2.1</Version>
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+      <PrivateAssets>all</PrivateAssets>
+    </PackageReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
     <PropertyGroup>
diff --git a/Source/MASVesselComputerModules.cs b/Source/MASVesselComputerModules.cs
index f47d7ecf..bd4ebb16 100644
--- a/Source/MASVesselComputerModules.cs
+++ b/Source/MASVesselComputerModules.cs
@@ -1339,11 +1339,14 @@ private void UpdateGear()
                 float numWheels = 0.0f;
                 for (int i = moduleWheelDeployment.Length - 1; i >= 0; --i)
                 {
-                    if (!moduleWheelDamage[i].isDamaged)
+                    var deploymentModule = moduleWheelDeployment[i];
+                    var damageModule = deploymentModule.wheelBase?.wheelDamageSubmodule;
+
+                    if (damageModule == null || !damageModule.isDamaged)
                     {
                         numWheels += 1.0f;
 
-                        newPosition += Mathf.InverseLerp(moduleWheelDeployment[i].retractedPosition, moduleWheelDeployment[i].deployedPosition, moduleWheelDeployment[i].position);
+                        newPosition += Mathf.InverseLerp(deploymentModule.retractedPosition, deploymentModule.deployedPosition, deploymentModule.position);
                     }
                 }