Skip to content

Commit

Permalink
Implement faster lookup of PartModules.
Browse files Browse the repository at this point in the history
This means adding a dependency for KSPCF though.
  • Loading branch information
siimav committed Oct 16, 2024
1 parent c769cbd commit c1429f8
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 142 deletions.
20 changes: 8 additions & 12 deletions FerramAerospaceResearch/FARAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You should have received a copy of the GNU General Public License
using ferram4;
using FerramAerospaceResearch.FARAeroComponents;
using FerramAerospaceResearch.FARGUI.FARFlightGUI;
using KSPCommunityFixes;
using UnityEngine;

// ReSharper disable UnusedMember.Global
Expand Down Expand Up @@ -222,9 +223,8 @@ public static void VesselIncreaseFlapDeflection(Vessel v)
{
foreach (Part p in v.parts)
{
if (!p.Modules.Contains<FARControllableSurface>())
if (!(p.FindModuleImplementingFast<FARControllableSurface>() is FARControllableSurface surface))
continue;
FARControllableSurface surface = p.Modules.GetModule<FARControllableSurface>();
surface.SetDeflection(surface.flapDeflectionLevel + 1);
}
}
Expand All @@ -236,9 +236,8 @@ public static void VesselDecreaseFlapDeflection(Vessel v)
{
foreach (Part p in v.parts)
{
if (!p.Modules.Contains<FARControllableSurface>())
if (!(p.FindModuleImplementingFast<FARControllableSurface>() is FARControllableSurface surface))
continue;
FARControllableSurface surface = p.Modules.GetModule<FARControllableSurface>();
surface.SetDeflection(surface.flapDeflectionLevel - 1);
}
}
Expand All @@ -252,9 +251,8 @@ public static int VesselFlapSetting(Vessel v)
{
foreach (Part p in v.parts)
{
if (!p.Modules.Contains<FARControllableSurface>())
if (!(p.FindModuleImplementingFast<FARControllableSurface>() is FARControllableSurface surface))
continue;
FARControllableSurface surface = p.Modules.GetModule<FARControllableSurface>();
if (surface.isFlap)
return surface.flapDeflectionLevel;
}
Expand All @@ -269,9 +267,8 @@ public static void VesselSetSpoilers(Vessel v, bool spoilerActive)
{
foreach (Part p in v.parts)
{
if (!p.Modules.Contains<FARControllableSurface>())
if (!(p.FindModuleImplementingFast<FARControllableSurface>() is FARControllableSurface surface))
continue;
FARControllableSurface surface = p.Modules.GetModule<FARControllableSurface>();
surface.brake = spoilerActive;
}
}
Expand All @@ -285,9 +282,8 @@ public static bool VesselSpoilerSetting(Vessel v)
{
foreach (Part p in v.parts)
{
if (!p.Modules.Contains<FARControllableSurface>())
if (!(p.FindModuleImplementingFast<FARControllableSurface>() is FARControllableSurface surface))
continue;
FARControllableSurface surface = p.Modules.GetModule<FARControllableSurface>();
if (surface.isSpoiler)
return surface.brake;
}
Expand Down Expand Up @@ -430,7 +426,7 @@ public static bool VesselVoxelizationCompletedAndValid(Vessel vessel)
/// Signature is (part, (drag, lift, torque)) -> (new_drag, new_lift, new_torque)</param>
public static void SetPartAeroForceModifier(Part part, Func<Part, Vector3, Vector3> modifier)
{
part.FindModuleImplementing<FARAeroPartModule>().AeroForceModifier = modifier;
part.FindModuleImplementingFast<FARAeroPartModule>().AeroForceModifier = modifier;
}

/// <summary>
Expand All @@ -441,7 +437,7 @@ public static void SetPartAeroForceModifier(Part part, Func<Part, Vector3, Vecto
/// Signature is (part, (drag, lift, torque)) -> (new_drag, new_lift, new_torque)</returns>
public static Func<Part, Vector3, Vector3> GetPartAeroForceModifier(Part part)
{
return part.FindModuleImplementing<FARAeroPartModule>()?.AeroForceModifier;
return part.FindModuleImplementingFast<FARAeroPartModule>()?.AeroForceModifier;
}
}
}
20 changes: 9 additions & 11 deletions FerramAerospaceResearch/FARAeroComponents/FARAeroPartModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ You should have received a copy of the GNU General Public License
using FerramAerospaceResearch.RealChuteLite;
using FerramAerospaceResearch.Settings;
using KSP.Localization;
using KSPCommunityFixes;
using UnityEngine;

namespace FerramAerospaceResearch.FARAeroComponents
Expand Down Expand Up @@ -238,8 +239,8 @@ public void SetProjectedArea(ProjectedArea areas, Matrix4x4 vesselToWorldMatrix)
double areaForStress = projectedArea.totalArea / 6;
if (!FARDebugValues.allowStructuralFailures ||
areaForStress <= 0.1 ||
part.Modules.Contains<RealChuteFAR>() ||
part.Modules.Contains<ModuleAblator>())
part.HasModuleImplementingFast<RealChuteFAR>() ||
part.HasModuleImplementingFast<ModuleAblator>())
{
partForceMaxY = double.MaxValue;
partForceMaxXZ = double.MaxValue;
Expand Down Expand Up @@ -303,17 +304,15 @@ private void Start()
partTransform = part.partTransform;

materialColorUpdater = new MaterialColorUpdater(partTransform, PhysicsGlobals.TemperaturePropertyID);
if (part.Modules.Contains<FARWingAerodynamicModel>())
LegacyWingModel = part.Modules.GetModule<FARWingAerodynamicModel>();
else if (part.Modules.Contains<FARControllableSurface>())
LegacyWingModel = part.Modules.GetModule<FARControllableSurface>();
if (part.FindModuleImplementingFast<FARWingAerodynamicModel>() is FARWingAerodynamicModel pm)
LegacyWingModel = pm;
else if (part.FindModuleImplementingFast<FARControllableSurface>() is FARControllableSurface pm2)
LegacyWingModel = pm2;
else
LegacyWingModel = null;

// For handling airbrakes aero visualization
stockAeroSurfaceModule = part.Modules.Contains<ModuleAeroSurface>()
? part.Modules.GetModule<ModuleAeroSurface>()
: null;
stockAeroSurfaceModule = part.FindModuleImplementingFast<ModuleAeroSurface>();
}

public double ProjectedAreaWorld(Vector3 normalizedDirectionVector)
Expand Down Expand Up @@ -581,9 +580,8 @@ private void CheckAeroStressFailure()
private void ApplyAeroStressFailure()
{
bool failureOccured = false;
if (part.Modules.Contains<ModuleProceduralFairing>())
if (part.FindModuleImplementingFast<ModuleProceduralFairing>() is ModuleProceduralFairing fairing)
{
ModuleProceduralFairing fairing = part.Modules.GetModule<ModuleProceduralFairing>();
fairing.ejectionForce = 0.5f;

fairing.DeployFairing();
Expand Down
7 changes: 4 additions & 3 deletions FerramAerospaceResearch/FARAeroComponents/FARVesselAero.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ You should have received a copy of the GNU General Public License
using FerramAerospaceResearch.FARGUI.FARFlightGUI;
using FerramAerospaceResearch.FARPartGeometry;
using FerramAerospaceResearch.FARThreading;
using KSPCommunityFixes;
using UnityEngine;

namespace FerramAerospaceResearch.FARAeroComponents
Expand Down Expand Up @@ -145,7 +146,7 @@ protected override void OnStart()
geoModulesReady++;
}

if (!p.Modules.Contains<KerbalEVA>() && !p.Modules.Contains<FlagSite>())
if (!p.HasModuleImplementingFast<KerbalEVA>() && !p.HasModuleImplementingFast<FlagSite>())
continue;
FARLogger.Info("Handling Stuff for KerbalEVA / Flag");
g = (GeometryPartModule)p.AddModule("GeometryPartModule");
Expand Down Expand Up @@ -435,7 +436,7 @@ public void VesselUpdate(bool recalcGeoModules)

_updateRateLimiter = 0;
_updateQueued = false;
if (vessel.rootPart.Modules.Contains<LaunchClamp>())
if (vessel.rootPart.HasModuleImplementingFast<LaunchClamp>())
{
DisableModule();
return;
Expand All @@ -447,7 +448,7 @@ public void VesselUpdate(bool recalcGeoModules)
geoModulesReady = 0;
foreach (Part p in vessel.Parts)
{
GeometryPartModule g = p.Modules.GetModule<GeometryPartModule>();
GeometryPartModule g = p.FindModuleImplementingFast<GeometryPartModule>();
if (g is null)
continue;
_currentGeoModules.Add(g);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ You should have received a copy of the GNU General Public License
using System;
using FerramAerospaceResearch.Settings;
using ModularFI;
using KSPCommunityFixes;
using UnityEngine;

namespace FerramAerospaceResearch.FARAeroComponents
Expand Down Expand Up @@ -75,7 +76,7 @@ private static void UpdateThermodynamicsPre(ModularFlightIntegrator fi)
{
PartThermalData ptd = fi.partThermalDataList[i];
Part part = ptd.part;
FARAeroPartModule aeroModule = part.Modules.GetModule<FARAeroPartModule>();
FARAeroPartModule aeroModule = part.FindModuleImplementingFast<FARAeroPartModule>();
if (aeroModule is null)
continue;

Expand Down Expand Up @@ -106,7 +107,7 @@ private static void UpdateThermodynamicsPre(ModularFlightIntegrator fi)
private static void UpdateAerodynamics(ModularFlightIntegrator fi, Part part)
{
//FIXME Proper model for airbrakes
if (part.Modules.Contains<ModuleAeroSurface>() ||
if (part.HasModuleImplementingFast<ModuleAeroSurface>() ||
part.Modules.Contains("MissileLauncher") && part.vessel.rootPart == part)
{
fi.BaseFIUpdateAerodynamics(part);
Expand Down Expand Up @@ -185,7 +186,7 @@ private static void CalculateLocalDynPresAndAngularDrag(ModularFlightIntegrator

private static double CalculateAreaRadiative(ModularFlightIntegrator fi, Part part)
{
FARAeroPartModule module = part.Modules.GetModule<FARAeroPartModule>();
FARAeroPartModule module = part.FindModuleImplementingFast<FARAeroPartModule>();
return CalculateAreaRadiative(fi, part, module);
}

Expand All @@ -204,7 +205,7 @@ FARAeroPartModule aeroModule

private static double CalculateAreaExposed(ModularFlightIntegrator fi, Part part)
{
FARAeroPartModule module = part.Modules.GetModule<FARAeroPartModule>();
FARAeroPartModule module = part.FindModuleImplementingFast<FARAeroPartModule>();
return CalculateAreaExposed(fi, part, module);
}

Expand All @@ -224,7 +225,7 @@ private static double CalculateAreaExposed(ModularFlightIntegrator fi, Part part

private static double CalculateSunArea(ModularFlightIntegrator fi, PartThermalData ptd)
{
FARAeroPartModule module = ptd.part.Modules.GetModule<FARAeroPartModule>();
FARAeroPartModule module = ptd.part.FindModuleImplementingFast<FARAeroPartModule>();

if (module is null)
return fi.BaseFIGetSunArea(ptd);
Expand All @@ -235,7 +236,7 @@ private static double CalculateSunArea(ModularFlightIntegrator fi, PartThermalDa

private static double CalculateBodyArea(ModularFlightIntegrator fi, PartThermalData ptd)
{
FARAeroPartModule module = ptd.part.Modules.GetModule<FARAeroPartModule>();
FARAeroPartModule module = ptd.part.FindModuleImplementingFast<FARAeroPartModule>();

if (module is null)
return fi.BaseFIBodyArea(ptd);
Expand Down
39 changes: 15 additions & 24 deletions FerramAerospaceResearch/FARAeroComponents/VehicleAerodynamics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You should have received a copy of the GNU General Public License
using FerramAerospaceResearch.FARPartGeometry;
using FerramAerospaceResearch.FARPartGeometry.GeometryModification;
using FerramAerospaceResearch.FARThreading;
using KSPCommunityFixes;
using UnityEngine;

namespace FerramAerospaceResearch.FARAeroComponents
Expand Down Expand Up @@ -223,23 +224,17 @@ private List<FARWingAerodynamicModel> LEGACY_UpdateWingAerodynamicModels()
Part p = aeroModule.part;
if (!p)
continue;
if (p.Modules.Contains<FARWingAerodynamicModel>())
if (p.FindModuleImplementingFast<FARWingAerodynamicModel>() is FARWingAerodynamicModel w)
{
var w = p.Modules.GetModule<FARWingAerodynamicModel>();
if (w is null)
continue;
w.isShielded = false;
w.NUFAR_ClearExposedAreaFactor();
_legacyWingModels.Add(w);
}
else if (p.Modules.Contains<FARControllableSurface>())
else if (p.FindModuleImplementingFast<FARControllableSurface>() is FARControllableSurface c)
{
FARWingAerodynamicModel w = p.Modules.GetModule<FARControllableSurface>();
if (w is null)
continue;
w.isShielded = false;
w.NUFAR_ClearExposedAreaFactor();
_legacyWingModels.Add(w);
c.isShielded = false;
c.NUFAR_ClearExposedAreaFactor();
_legacyWingModels.Add(c);
}
}

Expand Down Expand Up @@ -440,27 +435,26 @@ private Vector3 CalculateVehicleMainAxis()
continue;

// Could be left null if a launch clamp
var geoModule = p.Modules.GetModule<GeometryPartModule>();
var geoModule = p.FindModuleImplementingFast<GeometryPartModule>();

hitParts.Add(p);

Vector3 tmpCandVector = Vector3.zero;
Vector3 candVector = Vector3.zero;

//intakes are probably pointing in the direction we're gonna be going in
if (p.Modules.Contains<ModuleResourceIntake>())
if (p.FindModuleImplementingFast<ModuleResourceIntake>() is ModuleResourceIntake intake)
{
var intake = p.Modules.GetModule<ModuleResourceIntake>();
Transform intakeTrans = p.FindModelTransform(intake.intakeTransformName);
if (!(intakeTrans is null))
candVector = intakeTrans.TransformDirection(Vector3.forward);
}
//aggregate wings for later calc...
else if (geoModule == null ||
geoModule.IgnoreForMainAxis ||
p.Modules.Contains<FARWingAerodynamicModel>() ||
p.Modules.Contains<FARControllableSurface>() ||
p.Modules.Contains<ModuleWheelBase>() ||
p.HasModuleImplementingFast<FARWingAerodynamicModel>() ||
p.HasModuleImplementingFast<FARControllableSurface>() ||
p.HasModuleImplementingFast<ModuleWheelBase>() ||
p.Modules.Contains("KSPWheelBase"))
{
continue;
Expand Down Expand Up @@ -500,10 +494,9 @@ private Vector3 CalculateVehicleMainAxis()
hitParts.Add(q);

//intakes are probably pointing in the direction we're gonna be going in
if (q.Modules.Contains<ModuleResourceIntake>())
if (q.FindModuleImplementingFast<ModuleResourceIntake>() is ModuleResourceIntake intake2)
{
var intake = q.Modules.GetModule<ModuleResourceIntake>();
Transform intakeTrans = q.FindModelTransform(intake.intakeTransformName);
Transform intakeTrans = q.FindModelTransform(intake2.intakeTransformName);
if (!(intakeTrans is null))
candVector += intakeTrans.TransformDirection(Vector3.forward);
}
Expand Down Expand Up @@ -1457,12 +1450,10 @@ private void CalculateVesselAeroProperties()
if (key is null)
continue;

if (!key.Modules.Contains<FARAeroPartModule>())
if (!(key.FindModuleImplementingFast<FARAeroPartModule>() is FARAeroPartModule m))
continue;

var m = key.Modules.GetModule<FARAeroPartModule>();
if (!(m is null))
includedModules.Add(m);
includedModules.Add(m);

if (_moduleAndAreasDict.ContainsKey(m))
_moduleAndAreasDict[m] += areas;
Expand Down
11 changes: 5 additions & 6 deletions FerramAerospaceResearch/FARAeroComponents/VesselIntakeRamDrag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ You should have received a copy of the GNU General Public License

using System;
using System.Collections.Generic;
using KSPCommunityFixes;
using UnityEngine;

namespace FerramAerospaceResearch.FARAeroComponents
Expand Down Expand Up @@ -83,10 +84,9 @@ public void UpdateAeroData(List<FARAeroPartModule> allUsedAeroModules)
continue;
Part p = aeroModule.part;

foreach (PartModule m in p.Modules)
var intakeModules = p.FindModulesImplementingReadOnly<ModuleResourceIntake>();
foreach (ModuleResourceIntake intake in intakeModules)
{
if (!(m is ModuleResourceIntake intake))
continue;
if (intake.node != null && intake.node.attachedPart != null)
continue;

Expand All @@ -105,10 +105,9 @@ public void UpdateAeroData(List<FARAeroPartModule> allUsedAeroModules)
continue;
Part p = aeroModule.part;

foreach (PartModule m in p.Modules)
var engineModules = p.FindModulesImplementingReadOnly<ModuleEngines>();
foreach (ModuleEngines e in engineModules)
{
if (!(m is ModuleEngines e))
continue;
if (FARAeroUtil.AJELoaded)
if (e.ClassID == AJE_JET_CLASS_ID || e.ClassID == AJE_PROP_CLASS_ID)
{
Expand Down
3 changes: 2 additions & 1 deletion FerramAerospaceResearch/FARAeroUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ You should have received a copy of the GNU General Public License
using System.IO;
using ferram4;
using FerramAerospaceResearch.Settings;
using KSPCommunityFixes;
using UnityEngine;

namespace FerramAerospaceResearch
Expand Down Expand Up @@ -366,7 +367,7 @@ public static double MachBehindShockCalc(double M)
public static bool IsNonphysical(Part p)
{
return p.physicalSignificance == Part.PhysicalSignificance.NONE ||
p.Modules.Contains<LaunchClamp>() ||
p.HasModuleImplementingFast<LaunchClamp>() ||
HighLogic.LoadedSceneIsEditor &&
p != EditorLogic.RootPart &&
p.PhysicsSignificance == (int)Part.PhysicalSignificance.NONE;
Expand Down
Loading

0 comments on commit c1429f8

Please sign in to comment.